PHPTAL_Filter
This interface allows you to automatically filter templates sources (pre-filters) or PHPTAL result (post-filters).
Pre filters are invoked before the template parsing and won't be invoked until the source template file is modified.
Post filters are invoked after each template execution.
<?php
require_once 'PHPTAL.php';
class MyPreFilter implements PHPTAL_Filter {
public function filter($source){
return $source;
}
}
class MyPostFilter implements PHPTAL_Filter {
public function filter($xhtml){
return $xhtml;
}
}
$tpl = new PHPTAL('mytemplate.xhtml');
$tpl->setPostFilter(new MyPostFilter());
echo $tpl->execute();
?>
You can set only one post filter using setPostFilter()
. If you have more than one filter to chain, you can wrap them into a single class, implementing the PHPTAL_Filter
interface, which would invoke the filter's chain.
<?php
require_once 'PHPTAL.php';
class FilterChain implements PHPTAL_Filter {
private $_filters = array();
public function add(PHPTAL_Filter $filter){
$this->_filters[] = $filter;
}
public function filter($source){
foreach($this->_filters as $filter){
$source = $filter->filter($source);
}
return $source;
}
}
$myfilter = new FilterChain();
$myfilter->add(new CommentFilter()); // imaginary filter
$myfilter->add(new TidyFilter()); // imaginary filter
$tpl = new PHPTAL('mytemplate.xhtml');
$tpl->setPostFilter($myFilter);
echo $tpl->execute();
?>