Simple PHP Templates With PatTemplate - How to Process Template In PHP
(Page 3 of 6 )
Let's just jump right in and take a look at another example of using patTemplate:
<?PHP
// Include the patTemplate-file
include("includes/patTemplate.php");
// Initialize the patTemplate-class, and create an object
$tmpl = new patTemplate();
// Set which directory contains the template-files.
$tmpl->setBasedir("templates");
// Set which template-file to read.
$tmpl->readTemplatesFromFile("example1.tmpl.html");
// Add variables to the template
$tmpl->addVar("article", "HEADLINE", "This is the headline");
$tmpl->addVar("article", "CONTENT", "And this is the content...");
// Parse and display template
$tmpl->displayParsedTemplate("article");
?> As you can see from the example above, the way that we use patTemplate is fairly logical. Let's run through each part of the code shown above:
// Include the patTemplate-file
include("patTemplate.php"); This step is pretty self-explanatory. All this code does is include the patTemplate file.
// Initialize the patTemplate-class, and create an object
$tmpl = new patTemplate(); This code initializes the patTemplate class and create an object instance from it.
// Set which directory contains the template-files.
$tmpl->setBasedir("templates"); The setBasedir() function tells patTemplate where it can find the files containing the templates.
// Set which template-file to read.
$tmpl->readTemplatesFromFile("example1.tmpl.html"); The readTemplatesFromFile() function tells patTemplate which file it can find it's templates in.
// Add variables to the template
$tmpl->addVar("article", "HEADLINE", "This is the headline");
$tmpl->addVar("article", "CONTENT", "And this is the content..."); These two lines replace the placeholder variables with actual content.
The addVar() function takes three parameters:
addVar(template name, variable name and variable value) // Parse and display template
$tmpl->displayParsedTemplate("article"); This function parses and displays the template. You should now have a basic understanding on how patTemplate works. Let's go on to something more complicated.
A template loop Another interesting aspect of patTemplate is its ability to loop a template. For example, this could be used with a page where we iterate through a mysql-result.
Let's see an example:
<patTemplate:tmpl name="body">
<html>
<head>
<title>Entering a loop</title>
</head>
<body>
<table>
<patTemplate:tmpl name="namelist">
<tr>
<td>{NAME}</td>
</tr>
</patTemplate:tmpl>
</table>
</body>
</html>
</patTemplate:tmpl> You've already been given an explanation of how the template files work, so I won't go there again. You should save this template-file as "example2.tmpl.html".
Let's take a look at the PHP code, which is really the interesting part:
<?PHP
// Include the patTemplate-file
include("includes/patTemplate.php");
// Initialize the patTemplate-class, and create an object
$tmpl = new patTemplate();
// Set which directory contains the template-files.
$tmpl->setBasedir("templates");
// Set which template-file to read.
$tmpl->readTemplatesFromFile("example2.tmpl.html");
// Create an array to iterate through
$names = array("Paul", "John", "Roger");
// Iterate through the array
foreach ($names as $name) {
// Replace placeholder var
$tmpl->addVar("namelist", "NAME", $name);
$tmpl->parseTemplate("namelist", "a");
}
// Parse and display template
$tmpl->displayParsedTemplate("body");
?> There are a couple of new code segments in the example cove above, so let's take a look at them and what they do before moving on:
// Create an array to iterate through
$names = array("Paul", "John", "Roger"); We’ve just used PHP's array() function to create a new array called names so that we have something to iterate through.
// Iterate through the array
foreach ($names as $name) {
// Replace placeholder var
$tmpl->addVar("namelist", "NAME", $name);
$tmpl->parseTemplate("namelist", "a");
} This is where the real action is going on. There's nothing special about the addVar() function. The thing to note is the parseTemplate() function. The last parameter is "a", which stands for "append". This means that instead of just parsing the template all over and rewriting what's been parsed before, we actually append after what's already been parsed.
// Parse and display template
$tmpl->displayParsedTemplate("body"); The template "namelist" is a child of the "body" template because it's inside of it. When we call the displayParsedTemplate() function on the parent, all child-templates within it are displayed too.
Next: Different Template Types >>
More PHP Articles
More By Havard Lindset