Home arrow PHP arrow Page 4 - Simple PHP Templates With PatTemplate
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.

Author Info:
By: Havard Lindset
Rating: 5 stars5 stars5 stars5 stars5 stars / 217
June 26, 2002
TABLE OF CONTENTS:
  1. · Simple PHP Templates With PatTemplate
  2. · An Overview Of PatTemplate
  3. · How to Process Template In PHP
  4. · Different Template Types
  5. · Template Visibility
  6. · Conclusion

print this article
SEARCH DEVARTICLES

Simple PHP Templates With PatTemplate - Different Template Types
(Page 4 of 6 )

Another interesting thing that you can do with patTemplate is choose different types for your templates. This is done by adding an attribute called type to the <patTemplate:tmpl> tag.

There are four different types to choose from. I'll explain what each of them does with extracts from the patTemplate documentation.

Standard
This is the default type. Nothing special about it.

OddEven
When you assign this type to a template, you can insert two subtemplates (<patTemplate:sub>). They will then alternate when the template is repeated.

Condition
With this type you can assign as many subtemplates as you want. You have to assign one variable that will be compared with all the conditions of the subtemplates. There are two special conditions: default (similar to the default in a switch statement in PHP), and empty, which will be displayed when there is no value in the condition variable.

SimpleCondition
By using the SimpleCondition you can assign a list of variables (using the requiredvars) that have to be set if the template has to be displayed. If one of these variables isn't set, the template won't be visible.

Let's now go into depth for each one of these different types, exclusing the standard type, which we've already seen two examples of.

OddEven Type
Have you ever seen a table with two alterning colors? This can be done easily with patTemplate using the OddEven type - without any hardcoding!

<patTemplate:tmpl name="body">
<html>
<head>
<title>Alternating colors</title>
</head>
<body>
<table>
<patTemplate:tmpl name="namelist" type="OddEven">
<patTemplate:sub condition="odd">
<tr>
<td bgcolor="#EBEEF3">{NAME}</td>
</tr>
</patTemplate:sub>
<patTemplate:sub condition="even">
<tr>
<td bgcolor="#FFF2CC">{NAME}</td>
</tr>
</patTemplate:sub>
</patTemplate:tmpl>
</table>
</body>
</html>
</patTemplate:tmpl>


What we have here isn't really that complicated. We have the normal <patTemplate:tmpl> tag, only now it contains the "OddEven" attribute. Inside the tag there are two subtemplates that can be alternated. You can probably see that they have two different conditions, "Odd" and "Even".

Let's take a look at the PHP code:

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

// Create an array of names
$names = array("NAME" => array("Paul", "John", "Roger", "Michael", "Sarah", "Michelle"));

// Add the array
$tmpl->addVars("namelist", $names);
$tmpl->parseTemplate("namelist");

// Parse and display template
$tmpl->displayParsedTemplate("body");
?>


The only thing that needs explaining here are the array and the addVars() function.

The array $names is a multidimensional array. It's indexed by the variable name, which in this case is name. If I had more variables I could do it like this:

array("NAME" => array("Paul", "John", "Roger", "Michael", "Sarah", "Michelle"), "OTHERVAR" => array("value1", "value2"));

The addVars() function is simply a function that accepts an array with many variables at once.

Condition type
You can compare the condition type to a switch statement in PHP. It compares the conditionvar with different subtemplate-conditions. Here is an example:

<patTemplate:tmpl name="body">
<html>
<head>
<title>Visiting conditions</title>
</head>
<body>
<form action="example4.php">
<select name="name">
<option value="Bob" SELECTED>Bob</option>
<option value="Janice">Janice</option>
<option value="Morgan">Morgan</option>
</select>
<input type="submit">
</form>
<patTemplate:tmpl name="greeter" type="condition" conditionvar="NAME">
<patTemplate:sub condition="Bob">
Welcome chairman Bob. How are you today?
</patTemplate:sub>
<patTemplate:sub condition="Janice">
Well hello, Janice the janitor. Mop the floors immediately!
</patTemplate:sub>
<patTemplate:sub condition="default">
Well hello, guest.. What's your name?
</patTemplate:sub>
<patTemplate:sub condition="empty">
I see nobody! Where are you?
</patTemplate:sub>
</patTemplate:tmpl>
</body>
</html>
</patTemplate:tmpl>


As you can see, this is actually pretty easy. Basically you're given a lot of options for the conditionvar through the subtemplates. If the condition matches the conditionvar, then the subtemplate is shown. Here is the PHP code for this example:

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

// Add the condition variable from what was selected in the dropdown box.
$tmpl->addVar("greeter", "NAME", $_GET['name']);
$tmpl->parseTemplate("greeter");

// Parse and display template
$tmpl->displayParsedTemplate("body");
?>


By now you should probably understand what all of the above PHP code does. The addVar() line adds the conditionvar, and it gets it's value from the $_GET form array.

SimpleCondition type
When you're using the SimpleCondition type, you supply a list of variables (through the requiredvars attribute) that are needed for the template to be shown.

Here is an example where the variable NAME is required.

<patTemplate:tmpl name="body">
<html>
<head>
<title>SimpleCondition</title>
</head>
<body>
<patTemplate:tmpl name="varcheck" type="SimpleCondition" requiredvars="NAME">
I am happy to announce that the variable "NAME" has been set with the value "{NAME}".
</patTemplate:tmpl>
</body>
</html>
</patTemplate:tmpl>


If the variable NAME isn't set, then message inside the SimpleCondition template won't be visible. Here is the PHP code:

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

// Add the required variable
$tmpl->addVar("varcheck", "NAME", "Rachel");
$tmpl->parseTemplate("varcheck");

// Parse and display template
$tmpl->displayParsedTemplate("body");
?>


You can try commenting out the addVar() line to see for yourself that the message won't be displayed.
blog comments powered by Disqus
PHP ARTICLES

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials