Let's start with the page template:
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Page Title</title>
<style type="text/css">
<!--
body {
background-color: #000033;
}
.wrapper {
width: 800px;
margin: 0px auto;
background-color: #333333;
color: #FFFFFF;
font-family: Arial, Helvetica, sans-serif;
}
.header {
height: 100px;
width: 100%;
border: 2px inset #000000;
background-color: #666666;
color: #0000CC;
font-size: 36px;
font-weight: bold;
text-align: center;
}
.footer {
width: 100%;
background-color: #000000;
color: #CCCCCC;
font-size: 10px;
text-align: center;
}
-->
</style>
</head>
<body>
<div class="wrapper">
<div class="header">Header</div>
<div>Content</div>
<div class="footer">Footer Text</div>
</div>
</body>
</html>
Includes
The first method it the most basic, and it involves splitting the template into two pieces and including each half in the top and bottom.
First, you'll need to break your template into two files like this:
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Page Title</title>
<style type="text/css">
<!--
body {
background-color: #000033;
}
.wrapper {
width: 800px;
margin: 0px auto;
background-color: #333333;
color: #FFFFFF;
font-family: Arial, Helvetica, sans-serif;
}
.header {
height: 100px;
width: 100%;
border: 2px inset #000000;
background-color: #666666;
color: #0000CC;
font-size: 36px;
font-weight: bold;
text-align: center;
}
.footer {
width: 100%;
background-color: #000000;
color: #CCCCCC;
font-size: 10px;
text-align: center;
}
-->
</style>
</head>
<body>
<div class="wrapper">
<div class="header">Header</div>
<div>
Code: Select all
</div>
<div class="footer">Footer Text</div>
</div>
</body>
</html>
Using this is very simple using the include function:
Code: Select all
<?php
// the top half
include('template_head.htm');
// content goes here
echo 'Content';
// the bottom half
include('template_foot.htm');
?>
Smarter Includes
Includes are actually designed to execute code from external files rather than including static files. This allows you to use PHP files as your template. Here is what that will look like:
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $title; ?></title>
<style type="text/css">
<!--
body {
background-color: #000033;
}
.wrapper {
width: 800px;
margin: 0px auto;
background-color: #333333;
color: #FFFFFF;
font-family: Arial, Helvetica, sans-serif;
}
.header {
height: 100px;
width: 100%;
border: 2px inset #000000;
background-color: #666666;
color: #0000CC;
font-size: 36px;
font-weight: bold;
text-align: center;
}
.footer {
width: 100%;
background-color: #000000;
color: #CCCCCC;
font-size: 10px;
text-align: center;
}
-->
</style>
</head>
<body>
<div class="wrapper">
<div class="header">Header</div>
<div><?php echo $content; ?></div>
<div class="footer">Footer Text</div>
</div>
</body>
</html>
As you can see, I replaced the page title and content with PHP code that outputs variables. You can use code like this to set the variables and include the template:
Code: Select all
<?php
// set up content
$title = 'Custom Title';
$content = 'Page Content';
// output the template with variables set
include('template.php');
?>
Template Keywords
This method is my preferred method. Instead of includes it uses string functions, which are slightly more complicated and useful at the same time. Here is what the template would look like:
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>{TITLE}</title>
<style type="text/css">
<!--
body {
background-color: #000033;
}
.wrapper {
width: 800px;
margin: 0px auto;
background-color: #333333;
color: #FFFFFF;
font-family: Arial, Helvetica, sans-serif;
}
.header {
height: 100px;
width: 100%;
border: 2px inset #000000;
background-color: #666666;
color: #0000CC;
font-size: 36px;
font-weight: bold;
text-align: center;
}
.footer {
width: 100%;
background-color: #000000;
color: #CCCCCC;
font-size: 10px;
text-align: center;
}
-->
</style>
</head>
<body>
<div class="wrapper">
<div class="header">Header</div>
<div>{CONTENT}</div>
<div class="footer">Footer Text</div>
</div>
</body>
</html>
Code: Select all
<?php
$page_title = "Home";
if(is_file("template.htm")){
$template = file_get_contents("template.htm");
$template = str_replace('{TITLE}', $page_title, $template);
$template = explode('{CONTENT}', $template);
}
echo $template[0];
// content of page
echo 'Hello!';
echo $template[1];
?>
Code: Select all
$template = file_get_contents("template.htm");
Code: Select all
$template = str_replace('{TITLE}', $page_title, $template);
Code: Select all
$template = explode('{CONTENT}', $template);