Home arrow PHP arrow Page 2 - 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 - An Overview Of PatTemplate
(Page 2 of 6 )

The first thing I noticed about patTemplate was it's ease of use. When you make your templates, you divide your document into different parts by using XML-tags. Here is an example of a template:

<patTemplate:tmpl name="article">
<html>
<head>
<title>A patTemplate example</title>
</head>
<body>
<h3>{HEADLINE}</h3>
{CONTENT}
</body>
</html>
</patTemplate:tmpl>


When patTemplate parses this file, it looks for variables and replaces them with their associated values. In this case {HEADLINE} and {CONTENT} are the variables. We will see how we can assign values to these placeholder variable on the next page. Right now we will see how easy designing a template can be.

Here's some of the key-features of patTemplate (taken directly from the patTemplate site):
  • Use special XML tags to separate a document into templates
  • Use an infinite amount of templates in one page
  • Read from several plain html files
  • Local and/or global variables
  • Automatic repetition of templates to build lists
  • Automatically create alternating lists by using OddEven templates
  • Automatically enumerate entries in list
  • Templates can have child templates
  • Subtemplates to do simple if(); elseif(); else(); statements
  • Strip or replace unused variables from HTML
  • Hide/Show templates in a document
  • Read external files with/without parsing them
  • Strip leading/trailing whitespace in lines for smaller HTML code
  • Use a template several times in a document by linking to it
  • Quote template tags to use them in a second parsing process (enables you to build templates from templates)
  • Output any ASCII format you like (HTML, txt, XML, LaTex, etc)
A basic example
Let's take another look at the example we saw earlier. It's pretty basic, but I'll explain every part of it:

<patTemplate:tmpl name="article">
<html>
<head>
<title>A patTemplate example</title>
</head>
<body>
<h3>{HEADLINE}</h3>
{CONTENT}
</body>
</html>
</patTemplate:tmpl>


Let me explain the different parts of this example.

<patTemplate:tmpl name="table">

This is a special patTemplate tag that defines a template. The attribute "name" sets the name of the template, which by the way has to be unique as the patTemplate makes calls to different templates using their unique names.

{HEADLINE}

This is a variable. Variables works as placeholders for the actual content. I should probably mention that variables can only contain uppercase characters, numbers and the underscore (_). They are always enclosed in curly braces.

</patTemplate:tmpl>

This is yet another patTemplate tag which marks the end of a template. If you're used to XML then should be pretty self-explanatory.

In the next section we'll see how we can replace the placeholders with actual content. Before we continue, save the example above as "example1.tmpl.html".
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