whenpenguinsattack.com

Wednesday, March 08, 2006

patTemplate and php

by Justin Silverton

What is patTemplate?

patTemplate is a set of classes that allow you to separate your php code from design/output, making your code easier to maintain. To identify a certain part of the page as a template, patTemplate uses XML tags to assign a template a unique name and a various attributes (see patTemplate Tags and attributes for a list of all tags and attributes). When parsing a template, the parser divides the page in several chunks and treats them as separate templates. By using patTemplate's API you can hide, display or repeat a certain template.

Template Types

OddEven
you can assign to subtemplates () to this type: Odd and Even, which will be alternated, when the template is repeated

Condition
you can assign as many subtemplate as you like and you have to assign one variable which will be used to compare it with the specified conditions of the subtemplates. This allows you to define different HTML code for each value your condition variable may have. There are two special conditions you can use: default, which is similar to the default in a switch/case statement and empty which will be displayed, when there is no value assigned to the condition variable.

SimpleCondition
A situation that often arises is that an error message has to be displayed if an error occured. Normally you would set the error message if it exists or hide the template using setAttribute() if there is none. By using a template of type 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 is not set, the template will not be visible.

Variables

patTemplate supports variables similar to programming languages. There can be local variables (assigned by addVar) and global variables (assigned by addGlobalVar).

Global variables can only be scalar variables (strings, integers,...) and local variables can also be arrays. If you assign an array to a variable, the template where you assigned the variable will be repeated.

If a condition template uses the useglobals="yes" attribute global variables will be used if no local variables are set.

Variables have to be written in uppercase and may only contain chars, numbers or the underscore (_). Variables are always enclosed in {}.

Variables will be replaced with their values when parsing a template.

There is one predefined variable you can use when you want to enumerate a template that is automatically repeated: just place the variable PAT_ROW_VAR in your template, where you'd like to enumeration to appear (can also be used for javascript mouseovers).

How to install

There are two ways to install phptemplate

1) through pear

Download the pear package and execute: " pear install patTemplate-3.0.1.tgz"

2) Manually

A) Download the .zip or .tgz manual package from the patTemplate website
B) Download the patError class

Example of usage


Example of usage

error_reporting( E_ALL );

//require the error reporting class
require_once( 'pat/patErrorManager.php' );
//require the main template class
require_once '../patTemplate.php';
//create a new instance
$tmpl = &new patTemplate();
//set the directory of where to find the templates
$tmpl->setRoot( 'templates' );
//use this file for your template
$tmpl->readTemplatesFromInput( 'example_api_addvar.tmpl' );
//add a variable for displaying
$tmpl->addVar( 'template1', 'argh', array( 'one', 'two', 'repeat it.' ) );
//actually display it
$tmpl->displayParsedTemplate();

Why use this template system?

There are many template systems available for PHP. I have recently started using this one over the others because it is simple, has less overhead, and is much easier for a person designing your pages to read and understand.

Download

patTemplate can be downloaded Here

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home