PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Wed Nov 26, 2014 1:16 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Fri Feb 17, 2012 7:19 am 
Offline
Forum Contributor
User avatar

Joined: Wed Sep 09, 2009 5:55 am
Posts: 320
Location: Australia
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:
Syntax: [ Download ] [ Hide ]
$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.

Syntax: [ Download ] [ Hide ]
$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?


Top
 Profile  
 
PostPosted: Fri Feb 17, 2012 7:56 am 
Offline
Forum Newbie
User avatar

Joined: Wed Jan 18, 2012 7:35 am
Posts: 24
Location: London
just a suggestion but why don't use a templating engine such as smarty?


Top
 Profile  
 
PostPosted: Fri Feb 17, 2012 5:52 pm 
Offline
Forum Contributor
User avatar

Joined: Wed Sep 09, 2009 5:55 am
Posts: 320
Location: Australia
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.


Top
 Profile  
 
PostPosted: Fri Feb 17, 2012 6:59 pm 
Offline
Forum Contributor
User avatar

Joined: Wed Sep 09, 2009 5:55 am
Posts: 320
Location: Australia
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?


Top
 Profile  
 
PostPosted: Sat Feb 18, 2012 3:45 am 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 12696
Location: New York, NY, US
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.

_________________
(#10850)


Top
 Profile  
 
PostPosted: Mon Feb 20, 2012 8:22 pm 
Offline
Forum Contributor
User avatar

Joined: Wed Sep 09, 2009 5:55 am
Posts: 320
Location: Australia
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: [ Download ] [ Hide ]
  1. index.php 
  2. template/mytemplate.php 
  3. template/mytemplate/index.php 
  4. template/mytemplate/css/template.css 
  5. template/mytemplate/js/jquery.min.js 
  6. template/mytemplate/tpl/home.php 
  7. template/mytemplate/tpl/products.php 


The mytemplate.php contains the settings for the template.
Syntax: [ Download ] [ Hide ]
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.


Top
 Profile  
 
PostPosted: Fri Apr 13, 2012 11:53 am 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 12696
Location: New York, NY, US
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.

_________________
(#10850)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group