Here's a pretty lightweight way of having reusable elements to a page, but not the only way; you could just simply use 'includes'.
index.php
PHP Code:
<?php
error_reporting(-1);
ini_set('display_errors', true);
require_once 'lib/Template.php';
#base template
$page = new Template('lib/views/page.tpl');
$page->bind('title', 'Our Gallery');
#create a header
$header = new Template('lib/views/page.header.tpl');
#create the content
$content = new Template('lib/views/page.content.tpl');
$content->bind('name', 'Gallery');
#create the footer
$footer = new Template('lib/views/page.footer.tpl');
#assign the header, content and footer to the base template
$page->bind('header', $header);
$page->bind('content', $content);
$page->bind('footer', $footer);
#display it
echo $page;
lib/Template.php
PHP Code:
<?php
class Template
{
protected
$file;
protected
$data = array();
public function __construct($file){
$this->file = $file;
}
public function bind($key, $value){
$this->data[$key] = $value;
return $this;
}
public function render(){
ob_start();
extract($this->data);
include $this->file;
return ob_get_clean();
}
public function __toString(){
return $this->render();
}
}
lib/views/page.tpl
PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" media="screen" href="http://yui.yahooapis.com/3.3.0/build/cssreset/reset-min.css" />
<link rel="stylesheet" type="text/css" media="screen" href="http://yui.yahooapis.com/3.3.0/build/cssbase/base-min.css" />
<link rel="stylesheet" type="text/css" media="screen" href="http://yui.yahooapis.com/3.3.0/build/cssfonts/fonts-min.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<title>
<?php echo $title; ?>
</title>
</head>
<body>
<div id="site-container">
<?php echo $header; ?>
<?php echo $content; ?>
<?php echo $footer; ?>
</div>
</body>
</html>
lib/views/page.header.tpl
PHP Code:
<div id="site-header">
Header
</div>
lib/views/page.content.tpl
PHP Code:
<div id="site-content">
<h1>
This is the <?php echo $name; ?> page.
</h1>
</div>
lib/views/page.footer.tpl
PHP Code:
<div id="site-footer">
Footer
</div>
Bookmarks