Page 1 of 1

Most efficient way to include pages

Posted: Fri Feb 17, 2012 6:19 am
by Weiry
Long story short, I am trying to create a fairly simple template system where the user can code in standard html/php and the loading of pages/resources is handled through what im calling the "template" class.

I have most of everything i need already working however i have come to an impasse where trying to keep the template class as simple as possible for someone to create their own template and own pages.

The template class handles the collection of all css/js includes etc as well as the actual pages, so it would be fair to say that the management of every page needed by the template will be passing through this file.

Previously everything was handled by a very large switch statement with embedded ifelse statements inside each case to determine which sub-page will be used.

Short of actually doing this all again, there must be a better way of doing this.

Example of current setup:

Code: Select all

$p = $_GET['p']; $var = $_GET['var'];
switch($p){
    case "product":
        if($var == "cloud"){
            include_once 'tpl.cloud.php';
            break;
        }elseif($var == "tech" || $var == "robot"){
            include_once 'tpl.tech.html';
            break;
        }else{
            include_once 'tpl.products.html';
            break;
        }
    case "home":
    default:
        include_once 'tpl.home.php';
        break;
}
As you can see, this is a very poor method of including a large number of files.
My initial though was to implement something where you define the path for each page and associate a file with it.

Code: Select all

$pages = array(
    "/products" => "tpl.products.html",
    "/products/cloud" => "tpl.cloud.php",
    "/products/tech" => "tpl.tech.html",
    "/products/robot" => "tpl.tech.html",
    "/home" => "tpl.home.php",
    "/" => "tpl.home.php"
);
foreach($pages as $page => $file){
    // yes I realize this would cause problems, but its simplified for the sake of example
    // this would actually have to check for vars at each stage of the URL, adding more complexity.
    if($page == $_GET['p']."/".$_GET['var']){ 
        include_once "{$file}";
    }
The major downside to this is that for each page entered i have to manually perform a comparison directly with this array.
I am trying to come up with something between being efficient and readable.

Is there some sort of methodology or function that might be able to lend a hand to this as a factory method would to dynamic class creation?

Re: Most efficient way to include pages

Posted: Fri Feb 17, 2012 6:56 am
by theserve
just a suggestion but why don't use a templating engine such as smarty?

Re: Most efficient way to include pages

Posted: Fri Feb 17, 2012 4:52 pm
by Weiry
This is not a question about which template engine to use.
Even the smarty example uses a switch to define pages, which is where I am trying to find a better was of doing it.

At some point you still have to define the logic path of the template, what engine you use is completely up to you.
As with the example of sub pages, even if you have 4 effective levels of pages, logic must be written to determine how you get to those pages and with each level, you are adding infinatly more code if you attempt to do this via a switch within a switch as example.

Re: Most efficient way to include pages

Posted: Fri Feb 17, 2012 5:59 pm
by Weiry
I just came across Control Tables: Control Table
This seems to be a little similar to what i had thought of using with the associative array, but extended because of the logic as shown through CT4.

The only downside that i can see, that not only writing that would be more complicated, but it could also possibly introduce more overhead.

Other thoughts on how to write the logic path simply, but without huge control statements?

Re: Most efficient way to include pages

Posted: Sat Feb 18, 2012 2:45 am
by Christopher
What most frameworks use is to select by convention -- usually a parameter that maps to a file path. The you get to MVC which the VC part solves the problem you describe in a formal way.

Re: Most efficient way to include pages

Posted: Mon Feb 20, 2012 7:22 pm
by Weiry
Thanks Christopher,

I found some examples of MVC's and i have come up with a solution similar to how a standard MVC works.
The resource i found extremely helpful: Build a PHP MVC Framework in One Hour

While it doesn't parse the template files like smarty etc with its own built in tags, all php is coded normally inside the template.

File structure looks a bit like this:
[syntax]index.php
template/mytemplate.php
template/mytemplate/index.php
template/mytemplate/css/template.css
template/mytemplate/js/jquery.min.js
template/mytemplate/tpl/home.php
template/mytemplate/tpl/products.php[/syntax]

The mytemplate.php contains the settings for the template.

Code: Select all

require_once 'core/vn.template.php';

class mytemplate extends VNTemplate{

        /**
         * Details about the author of the template.
         */
        private $name           = 'mytemplate';
        private $creationDate   = '17th Feb 2012';
        private $author         = 'Weiry';
        private $authorEmail    = 'name@email.com';
        private $authorURL      = 'http://website.com';
        private $version        = '1.0';
        private $description    = 'First template design!';

        /**
         * Loads an array of includes to be printed into template if desired
         * So that management of template global details is easier
         * TODO: 
         *      Implement folder includes
         *      If "css", error on "folder" => "css" and vice versa
         */
        private $headers = array( //"css"  => array( "template.css", "style.css" ), //include css files (specific and ordered)
                                  //"js"   => array( "jquery.min.js" ), //include javascript files (specific and ordered)
                                  "fold" => array( "css", "js" ), //include folders (non-specific, ordered alphanumberic)
                                  "meta" => array( 
                                                  array("author", "Weiry" ), // problems occur when using $vars such as $this->author
                                                  array("description", "First template design!") //problems occur when using $vars such as $this->description
                                                  ), 
                                        
                                );
        /**
         * Optional -- Define the main menu if there are items with permission restrictions
         */
        private $mainMenu = array("home"          => array("text" => "Home",      "permission" => 1),
                                  "product"       => array("text" => "Product",   "permission" => 1),
                                  "tech"          => array("text" => "Tech",      "permission" => 1),
                                  "cloud"         => array("text" => "Cloud",     "permission" => 1),
                                  "logout"        => array("text" => "Logout",    "permission" => 2)); //logout menu item requires being logged in to be visible (permission 2+)

...
...
...
The idea behind this is sort of inspired by the format in which Joomla stores its template information in it's templateDetails.xml where you can define the files which it should know about as well as author information etc. Useful for when you have an administrative panel that has the ability to load specific templates.

URI formatting is: mydomain.com/index.php?home&var1=foo&var2=bar....
Actual URL: mydomain.com/home/foo/bar/....

Steps:
index.php constructs the base class (main constructor that runs everything)
base class reads core configuration (and reads template = 'mytemplate')
base constructor loads the template controller (mytemplate.php)
base constructor initializes the template via $this->controller->loadTemplate();
controller checks requested page and find if it exists (page = home), then loads it (/template/mytemplate/tpl/home.php).

There are a few issues during the whole process about some variables not being easily accessible to the template controller which im working on currently.

Re: Most efficient way to include pages

Posted: Fri Apr 13, 2012 11:53 am
by Christopher
Christopher wrote:What most frameworks use is to select by convention -- usually a parameter that maps to a file path. In MVC -- the VC part solves the problem you describe in a formal way.