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".
Next: How to Process Template In PHP >>
More PHP Articles
More By Havard Lindset