Home arrow PHP arrow Page 3 - 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 - 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.
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