I have a dilemma and would appreciate any of your opinions:
My question is this should I use the second option? or stick to the way the book has taught me. Having such a templating system surely would complicate things? Any comments appreciated thank you.
The way I have been learning php via text books has shown me to use the following method
<?php
Require_once(‘dataconnection.php’);
Includes(‘header.html’);
Content made of php
Includes(‘footer.html’);
?>
I have come across another example on the web, which is made of three pages + index page:
Template.php
<?php
# THIS FUNCTION SWAPS OUT THE {{{ }}} TAGS WITH YOUR CODE
function get_template($template_name, $contentArray) {
global $settings;
# GET THE TEMPLATE
$path = $settings["DOC_PATH"].'templates/'.$template_name.'.html';
if(file_exists($path)) {
$handle = fopen($path, "r");
$templateContent = @fread($handle, filesize($path));
fclose($handle);
} else {
echo "Error: unable to open the temlpate from ". $path;
exit; // if it cant find the template ... stop the script
}
# REPLACE ALL THE TAGS
// get the doc path and add it to the contentArray
$contentArray["DOC_PATH"] = $settings["DOC_PATH"];
if(is_array($contentArray)) {
foreach($contentArray as $key => $value) {
$templateContent = str_replace('{{{'.$key.'}}}', $value, $templateContent);
}
}
return $templateContent;
}
?>
Renderpage.php
<?php
function render_page($page) {
# print the page to screen
echo $page;
}
?>
Runner.php
<?php
# ERROR REPORTING
error_reporting (E_ERROR | E_PARSE | E_WARNING);
# DATABASE CONFIG
$hostname = "";
$username = "";
$password = "";
$type = "";
$databasename = "";
# SET UP THE DATABASE CONNECTION
# $database = mysql_connect($hostname, $username, $password);
# mysql_select_db($databasename) or die("Could not select the database");
# SET UP TO SEE IF WE ARE IN THE ROOT OR NOT AND SET THE DOCPATH
# WE CHECK AGAINST PREFS.PHP AS THE LOCATION OF THIS FILE SHOULD NEVER CHANGE
if(file_exists("lib/prefs.php")) {
$docPath = ""; // we are in the root
} else {
$docPath = "../"; // we are not in the root
}
# GET A LIST OF FILES IN THE LIB FOLDER AND LOAD THEM, WE USE THE DOCPATH SET ABOVE TO LOCATE THE FILES
if($handle = opendir($docPath."lib/")) {
while (false !== ($file = readdir($handle))) {
if($file != "." && $file != ".." && eregi('.php$',$file)) {
$file = trim($file);
require_once($docPath . "lib/" . $file);
}
}
closedir($handle);
}
unset($handle);
# PUT GENERAL SETTINGS INTO THE SETTINGS ARRAY
$settings['DOC_PATH'] = $docPath;
# some examples like set the table names to be queried
# settings['users_table'] = "x_users"
?>
Index.php
<?
# START SESSION
session_start();
# REQUIRE FILES
require_once("lib/prefs.php");
# SET GLOBALS
global $settings;
# HEADER
# BODY
switch($_REQUEST["view"]) {
##########################################################################################################
# DISPLAY STANDARD PAGE
##########################################################################################################
case "":
default:
# PAGE NUMBER
//$page = 1;
# HOME WELCOME
//$repTags1["EDITION_DATE"] = edition_date();
$repTags["HOMEWELCOME"] = get_template("home_welcome", &$repTags1);
unset($repTags1);
# WELCOME BANNER
//$repTags1["ARCHIVESNAV"] = archives($page);
$repTags["WELCOMEBANNER"] = get_template("home_welcome_banner", &$repTags1);
# MAIN CONTENT
//$repTags1["TITLE"] = content_title($page);
//$repTags1["STORY"] = content_story($page);
//$repTags["CONTENT"] = get_template("content_home", &$repTags1);
unset($repTags1);
# FOOTER
//$repTags1["COLOR"] = navColor($page);
$repTags["FOOTER"] = get_template("footer", &$repTags1);
unset($repTags1);
# FULL WEBPAGE
$path = $settings["DOC_PATH"];
$body = get_template("_standard_page", &$repTags);
unset($repTags);
break;
}
$page = $body;
render_page($page, &$textArray);
?>
My question is this should I use the second option? or stick to the way the book has taught me. Having such a templating system surely would complicate things? Any comments appreciated thank you.
The way I have been learning php via text books has shown me to use the following method
<?php
Require_once(‘dataconnection.php’);
Includes(‘header.html’);
Content made of php
Includes(‘footer.html’);
?>
I have come across another example on the web, which is made of three pages + index page:
Template.php
<?php
# THIS FUNCTION SWAPS OUT THE {{{ }}} TAGS WITH YOUR CODE
function get_template($template_name, $contentArray) {
global $settings;
# GET THE TEMPLATE
$path = $settings["DOC_PATH"].'templates/'.$template_name.'.html';
if(file_exists($path)) {
$handle = fopen($path, "r");
$templateContent = @fread($handle, filesize($path));
fclose($handle);
} else {
echo "Error: unable to open the temlpate from ". $path;
exit; // if it cant find the template ... stop the script
}
# REPLACE ALL THE TAGS
// get the doc path and add it to the contentArray
$contentArray["DOC_PATH"] = $settings["DOC_PATH"];
if(is_array($contentArray)) {
foreach($contentArray as $key => $value) {
$templateContent = str_replace('{{{'.$key.'}}}', $value, $templateContent);
}
}
return $templateContent;
}
?>
Renderpage.php
<?php
function render_page($page) {
# print the page to screen
echo $page;
}
?>
Runner.php
<?php
# ERROR REPORTING
error_reporting (E_ERROR | E_PARSE | E_WARNING);
# DATABASE CONFIG
$hostname = "";
$username = "";
$password = "";
$type = "";
$databasename = "";
# SET UP THE DATABASE CONNECTION
# $database = mysql_connect($hostname, $username, $password);
# mysql_select_db($databasename) or die("Could not select the database");
# SET UP TO SEE IF WE ARE IN THE ROOT OR NOT AND SET THE DOCPATH
# WE CHECK AGAINST PREFS.PHP AS THE LOCATION OF THIS FILE SHOULD NEVER CHANGE
if(file_exists("lib/prefs.php")) {
$docPath = ""; // we are in the root
} else {
$docPath = "../"; // we are not in the root
}
# GET A LIST OF FILES IN THE LIB FOLDER AND LOAD THEM, WE USE THE DOCPATH SET ABOVE TO LOCATE THE FILES
if($handle = opendir($docPath."lib/")) {
while (false !== ($file = readdir($handle))) {
if($file != "." && $file != ".." && eregi('.php$',$file)) {
$file = trim($file);
require_once($docPath . "lib/" . $file);
}
}
closedir($handle);
}
unset($handle);
# PUT GENERAL SETTINGS INTO THE SETTINGS ARRAY
$settings['DOC_PATH'] = $docPath;
# some examples like set the table names to be queried
# settings['users_table'] = "x_users"
?>
Index.php
<?
# START SESSION
session_start();
# REQUIRE FILES
require_once("lib/prefs.php");
# SET GLOBALS
global $settings;
# HEADER
# BODY
switch($_REQUEST["view"]) {
##########################################################################################################
# DISPLAY STANDARD PAGE
##########################################################################################################
case "":
default:
# PAGE NUMBER
//$page = 1;
# HOME WELCOME
//$repTags1["EDITION_DATE"] = edition_date();
$repTags["HOMEWELCOME"] = get_template("home_welcome", &$repTags1);
unset($repTags1);
# WELCOME BANNER
//$repTags1["ARCHIVESNAV"] = archives($page);
$repTags["WELCOMEBANNER"] = get_template("home_welcome_banner", &$repTags1);
# MAIN CONTENT
//$repTags1["TITLE"] = content_title($page);
//$repTags1["STORY"] = content_story($page);
//$repTags["CONTENT"] = get_template("content_home", &$repTags1);
unset($repTags1);
# FOOTER
//$repTags1["COLOR"] = navColor($page);
$repTags["FOOTER"] = get_template("footer", &$repTags1);
unset($repTags1);
# FULL WEBPAGE
$path = $settings["DOC_PATH"];
$body = get_template("_standard_page", &$repTags);
unset($repTags);
break;
}
$page = $body;
render_page($page, &$textArray);
?>