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
Working with output filters
Output filters can be used to transparently modify the resulting HTML that is generated by patTemplate. Output filters can either be applied to a single template or the complete HTML that is displayed when calling displayParsedTemplate().
Using an output filter for the generated page
To apply any output filter to all generated content, you may use the applyOutputFilter() method, which accepts the following arguments:
- Name of the filter
- Optional associative array containing options
The following example would apply the Tidy output filter (requires ext/tidy) which can be used to create cleaner HTML:
$tmpl = new patTemplate();
$options = array(
'output-xhtml' => true,
'clean' => true
);
$tmpl->applyOutputFilter('Tidy', $options);
$tmpl->readTemplatesFromInput('myPage.tmpl');
$tmpl->displayParsedTemplate();
You can apply more than one output filter, they will be executed in the order they have been applied.
What about getParsedTemplate()
When using getParsedTemplate(), the output filters will not be applied, unless you supply true as second parameter:
$tmpl = new patTemplate();
$tmpl->setRoot('templates');
$tmpl->applyOutputFilter('StripWhitespace');
$tmpl->readTemplatesFromInput('myPage.tmpl');
// Will not strip whitespace
$html = $tmpl->getParsedTemplate('page');
// Will strip whitespace
$compressed = $tmpl->getParsedTemplate('page', true);
Applying an output filter to only one template
If you want to apply an output filter only to one template, you can use the outputFilter attribute of the <patTemplate:tmpl/> tag:
<patTemplate:tmpl name="page" outputFilter="StripWhitespace"> The whitespace will alyways be stripped from this template. </patTemplate:tmpl>
The filter will be applied to this template, regardless of using displayParsedTemplate() or getParsedTemplate().
Available filters
The following filters are included in the distribution:
- BBCode, uses patBBCode to transform BBCode in the templates to HTML
- GZip, used to gzip pages before they are sent to the browser (cannot be used for only one template for obvious reasons)
- HighlightPhp, used to syntax-highlight PHP code in the templates (using highllight_string())
- PdfLatex, used to transform LaTeX documents to PDF (can be used to generate PDF with patTemplate)
- StripWhitespace, used to remove unneeded whitespace from documents
- Tidy, used to repair your HTML using the tidy extension.
All output filters are located in the patTemplate/OutputFilter/ folder. You can also easily implement new filters.
