TracNav menu
-
Documentation
- Introduction
- Creating templates
-
Adding variables...
- Using the simpleCondition template
- Creating conditional templates
- Creating alternating rows?
-
Advanced features
- Reading templates from different data sources
- Variable Modifiers
- Improving performance with template caches
- Output Filters
- Input Filters
- Tag Reference
- FAQ
- Home
-
Extending patTemplate...
- Download
Reading templates from various data sources
patTemplate is not limited to reading templates from the file system, but uses a driver based approach to read templates from virtually anywhere.
This driver-based approach has two benefits:
- Templates can be read from any data source
- Different drivers may parse a different template format
A driver to read templates is called a template reader. Currently, the following readers are available:
- String, reads templates from a string, can be used when your templates are created at runtime.
- File, reads templates from one or more files. This is the default reader.
- DB, reads templates from any database that is supported by PEAR::DB
- IT, reads templates in the HTML_Template_IT format from the filesystem
The reader to use can be specified when parseTemplatesFromInput() is called:
$tmpl = new patTemplate();
$tmpl->readTemplatesFromInput('MyTemplates.tmpl', 'File');
If you want to use the file reader, the reader name is optional.
Reading templates from file
Reading templates from a file is the most common task. You may use the setRoot() method to specify one or more folders, where the templates are located.
$tmpl = new patTemplate();
$tmpl->setRoot('/path/to/templates', 'File');
$tmpl->readTemplatesFromInput('myTemplate.tmpl');
Reading templates from a string
Reading a template from a string might be helpful, when you need to read a template from a source, that is not supported by patTemplate and implementing a new reader would be overkill.
All you need to do is pass the template source to the readTemplatesFromInput() method:
$string = '<patTemplate:tmpl name="string">This template has been parsed from a string <patTemplate:tmpl name="too">, as well as this.</patTemplate:tmpl></patTemplate:tmpl>'; $tmpl = new patTemplate(); $tmpl->readTemplatesFromInput( $string, 'String' );
Reading templates from a database
When reading your templates from a database, the PEAR::DB abstraction layer will be used. You need to pass the DSN string to the setRoot() method:
tmpl = new patTemplate();
$tmpl->setRoot('mysql://root:@localhost/test', 'DB');
Imagine, your templates are stored in a table called templates that has to fields:
- id, unique id for a template
- content, a text field containing the actual template
If you want to read a template called foo from the database, you may use to different ways. One would be passing the SQL statement to the readTemplatesFromInput() method:
$tmpl->readTemplatesFromInput("SELECT content FROM templates WHERE id='foo'", 'DB');
However, if your table is that simple, you can let the reader build the query for you and use an XPath-like syntax:
$tmpl->readTemplatesFromInput('templates[@id=foo]/@content', 'DB');
This string consists of the following parts:
$table[@$keyName=$keyValue]/@$templateField
Reading HTML_Template_IT templates
The IT reader allows you to read templates in the HTML_Template_IT or PHPLib syntax. The reader itself works exactly like the File reader.
