PHP

Simple PHP Templates With PatTemplate

PatTemplate is a template system that gives us the power and flexibility to add templates to our PHP driven sites. In this article Havard gives us details and examples of how to use patTemplate with PHP.

There comes a time when you want to hide templates from being showed. This is done by adding the visbility attribute to a template tag. The visibility attribute takes two options, show (default) and hidden. Here is an example where the "content" template is hidden:

<patTemplate:tmpl name="content"> This is the content. I will be displayed within the "body"-template.<br> Feel free to use variables inside this template too. Like this:<br><br> My name is {NAME}. </patTemplate:tmpl>

The link within the "body" template links to the template "content", and includes it. Pretty easy, eh? Linking templates can make things easier when you’re editing large template files.

Here is the PHP code I used:

<?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("example6.tmpl.html");

I usually split my templates up into three different files: Header, content and footer. This way I only have to update one file if I want to modify the header (instead of changing the header part in every single template-file).

Global variablesSo far we've only looked at patTemplate using local variables. There are two different ways to add a variable with patTemplate: through the local scope or through the global scope. Here's an example of global variables in action: