Most efficient way to include pages
Posted: Fri Feb 17, 2012 6:19 am
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:
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.
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?
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;
}
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}";
}
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?