class PHPTAL_PreFilter

Pre filters are executed only once before template is compiled. Pre filters operate on template's source code, so they are not able to access value of any template variables. However pre filters can "see" and modify TAL markup.

To create pre filter extend PHPTAL_PreFilter class and implement only filter *() methods you need.

filter()

Receives template source code as string and is expected to return new source.

You can use it to simply find'n'replace fragments of source code. Be careful not to introduce syntax errors in the template.

Warning

PHPTAL's error messages will refer to line numbers after filtering, which may be confusing if your prefilter adds or remove lines from the source code.

filterDOM()

Receives root PHPTAL DOM node of parsed file and should edit it in place.

Example pre filter that removes all comments:


            function filterDOM(PHPTAL_Dom_Element $element)
{
    foreach($element->childNodes as $node) {
       if ($node instanceof PHPTAL_Dom_Comment) {
           $node->parentNode->removeChild($node);
       }
       else if ($node instanceof PHPTAL_Dom_Element) {
           $this->filterDOM($node); /* recursively filter all elements */
       }
    }
}

          

getCacheId()

Should return (any) string that uniquely identifies this filter and its settings, which is used to (in)validate template cache. Each time you return different string, template will be recompiled. Implement this method if result of the filter depends on its configuration.

Unlike other filter methods, this one is called on every execution.

Tip

When developing and testing your filter, set setForceReparse(true) to force PHPTAL to update templates every time. Otherwise result of your filter will be cached and you won't see the changes.

getPHPTAL()

Returns instance of PHPTAL class that uses this prefilter. You can query it to check current encoding or other settings.