You are here: WikiStart/Docs/Developer


Extending patTemplate

Starting with version 3.0, patTemplate has a more abstracted architecture, which allows you to add new or exchange components without modifying the core. To extend patTemplate you'll have to understand its internal structure and class trees.

Directory structure

The patTemplate directory structure was modeled after the PEAR concept. The main class patTemplate is located in the file patTemplate.php in the root folder (or in /pat if you installed it using the PEAR installer). This is the only file you need to include directly. You will also find a directory patTemplate which contains all modules of patTemplate. In this directory there's a Module.php file, which contains the class patTemplate_Module that acts as a base class for all different kinds of modules like Functions, Modifiers, Readers, etc. Furthermore you'll find a file for each of the modules that can be used in patTemplate like Reader.php, Function.php, TemplateCache.php, etc. These files contain the base classes for the modules you may create. In the patTemplate folder there are several folders, which help you organize the actual modules. The readers are in patTemplate/Reader, the functions in patTemplate/Functions, etc. That means, if you create a new modifier that should be used in the templates as FooMod, you will have to place it in a file called patTemplate/Modifier/FooMod.php otherwise patTemplate will not be able to locate it.

Class trees

patTemplate is fully object oriented code. That means all patTemplate modules have to be classes. Most of the classes are quite simple and you only have to implement one or two methods. But nevertheless they have to be classes, as they must not pollute the global namespace. The files and directories map directly to the classnames. That means, patTemplate.php contains a class called patTemplate and the file patTemplate/Reader.php contains the class patTemplate_Reader. If you create a reader that should be able to read templates from a database, you should place it into patTemplate/Reader/DB.php and call the class patTemplate_Reader_DB. If you do not follow these simple rules, patTemplate will currently not be able to load and instantiate the module and your application will break.

Components

patTemplate consists of the following component types.

Variable Modifiers

Variable modifiers allow you to apply PHP functions and methods to variables without writing any PHP code. Once you provide a modifier, the template designer may easily use it in the templates. Variable modifiers may be used to apply htmlspecialchars() to your vars, as well as creating images and reversing text.

Template Readers

By default, patTemplate is reading templates from the local filesystem. But maybe you prefer storing the templates in a database or even shared memory. This is possible by creating a new Template Reader. Readers also allow you to 'assimilate' foreign template systems. patTemplate comes with a reader that is able to interpret HTML_Template_IT (and PHPLib) templates and treat them like normal patTemplate files.

Output Filters

Output Filters allow you to modify the resulting HTML code, before it is sent to the browser. Possible use cases are the removal of unneeded whitespace or even compressing the output, if the client supports gzipped content.

Input Filters

Input Filters are applied before the reader analyses the templates. This allows you to uncompress data, that is stored in gzipped format or remove comments that you do not need in the templates and thus improve the performance of the reader.

Custom Tags/Functions

Custom Functions allow you to create new tags for the patTemplate namespace. You have to write a class that will handle the tag whenever it occurs in the template. This enables you to dynamically include any kind of dynamically generated content in your templates.

Template Caches

The template cache will speed up you patTemplate powered sites. It will store the structures returnd by the reader in a serialized format that can be read faster than the original templates. patTemplate comes with a cache that stores the cache files in file system, but you can store them virtually anywhere.

Template Dumpers

The Dump helps you debugging you templates as it presents a human readable interpretation of the internal structures that includes all templates, conditions, modifiers but also the variables that have been set. The initial version features a DHTML Dump, but you may create a dump that displays plain text, XUL or whatever you like.