Created
November 16, 2014 20:42
-
-
Save dimo414/7bc616f7e5be7eae36bd to your computer and use it in GitHub Desktop.
Example code for creating a flexible PHP template with shared header and footer behavior.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Enables a PHP page to abstract out the site design (the template) from | |
the page design (the content). | |
Generally, a page will have a common header and footer (sidebars and the | |
like would live in one or the other). Following this pattern, a new page | |
can be created (after including the necessary files of course)) like so: | |
... request validation and business logic | |
$template->header("My Page Title"); | |
... body of page goes here | |
$template->footer(); | |
Note this class is very fail-soft, silently ignoring calls made after header() | |
or footer() are called, and so on. It would likely be better to refactor this | |
class to fail-fast when missued, or at least fail more loudly by logging errors | |
or warnings. | |
*/ | |
class template | |
{ | |
private $title = ''; | |
private $subtitle = ''; | |
private $meta = ''; | |
private $httpEquiv = ''; | |
private $extraStyleSheets = ''; | |
private $extraCSS = ''; | |
private $alternate = ''; // RSS Feeds and the like | |
private $onload = ''; | |
private $queue = ''; | |
function setTitle($title) | |
{ | |
$this->title = defined('HEADER_CALLED') ? $this->title : $title; | |
} | |
function setSubTitle($subtitle){ | |
$this->subtitle = defined('HEADER_CALLED') ? $this->subtitle : $subtitle; | |
} | |
function addMeta($name, $content) | |
{ | |
$this->meta .= defined('HEADER_CALLED') ? '' : '<meta name="'.$name.'" content="'.$content.'" />'."\n"; | |
} | |
function addHttpEquiv($name, $content) | |
{ | |
$this->httpEquiv .= (defined('HEADER_CALLED') ? '' : '<meta http-equiv="'.$name.'" content="'.$content.'" />'."\n"); | |
} | |
function addStyleSheet($url, $media = 'all') | |
{ | |
$this->extraStyleSheets .= (defined('HEADER_CALLED') ? '' : '<link rel="stylesheet" type="text/css" href="'.$url.'" media="'.$media.'" />'."\n"); | |
} | |
function addCSS($css) | |
{ | |
$this->extraCSS .= defined('HEADER_CALLED') ? '' : $css."\n"; | |
} | |
function extraCSS(){ | |
if($this->extraCSS != '') return '<style type="text/css">'.$this->extraCSS.'</style>'; | |
return ''; | |
} | |
function addAlternate($type, $title, $url) | |
{ | |
$this->alternate .= defined('HEADER_CALLED') ? '' : '<link rel="alternate" type="'.$type.'" title="'.$title.'" href="'.$url.'" />'."\n"; | |
} | |
function addOnload($code) | |
{ | |
$this->onload .= defined('HEADER_CALLED') ? '' : $code; | |
} | |
function toFirstField() | |
{ | |
$this->addOnload('document.forms[0][0].focus();'); | |
} | |
// to prevent error messages from being output before header is called, only echos content once header is called. otherwise it is stored | |
// and output as soon as header is called. | |
function output($content){ | |
if(defined('HEADER_CALLED')){ | |
echo $content; | |
return; | |
} | |
$this->queue .= $content; | |
} | |
// outputs an error message once header is called, or immediatly if header has already been called. | |
function error($message){ | |
$this->output('<div class="error">'.$message.'</div>'); | |
} | |
function header($title = '', $subtitle = '') | |
{ | |
if(!defined('HEADER_CALLED')) | |
{ | |
define('HEADER_CALLED', true); | |
$this->title = ($this->title == '') ? $title : $this->title; | |
$this->subtitle = ($this->subtitle == '') ? $subtitle : $this->subtitle; | |
include $_SERVER['DOCUMENT_ROOT']."/includes/header.inc.php"; | |
echo $this->queue; | |
} | |
} | |
function footer() | |
{ | |
if(!defined('FOOTER_CALLED')) | |
{ | |
define('FOOTER_CALLED', true); | |
include $_SERVER['DOCUMENT_ROOT']."/includes/footer.inc.php"; | |
} | |
} | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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" xml:lang="en" lang="en"> | |
<!-- | |
this is a minimal example of how to create a header that works with the | |
Template class, it can be changed and expanded as needed. | |
Note you should prefer hard-coding content in this file rather than | |
calling the methods of the Template class on every page - those methods | |
are intended for modifying the behavior of individual pages. | |
--> | |
<head> | |
<title><?php echo $this->title; ?></title> | |
<meta http-equiv="content-type" content="text/html;charset=UTF-8" /> | |
<?php echo $this->meta.$this->httpEquiv; ?> | |
<?php echo $this->alternate; ?> | |
<link rel="stylesheet" type="text/css" href="/template/style.css" /> | |
<?php echo $this->extraStyleSheets; ?> | |
<?php echo $this->extraCSS(); ?> | |
</head> | |
<body onload="<?php echo $this->onload; ?>"> | |
<div id="container"> | |
<div id="header"> | |
<h1><?php echo $this->title; ?></h1> | |
<h2><?php echo $this->subtitle; ?></h2> | |
</div> | |
<div id="content"> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment