Chapter 7. Advanced Features

Table of Contents
Prefilters
Postfilters
Cache Handler Function
Resources

Prefilters

Template prefilters are PHP functions that your templates are ran through before they are compiled. This is good for preprocessing your templates to remove unwanted comments, keeping an eye on what people are putting in their templates, etc. Prefilters are processed in the order they are registered or loaded from the plugin directory. Smarty will pass the template source code as the first argument, an expect the function to return the resulting template source code.

Example 7-1. using a template prefilter

<?php
// put this in your application
function remove_dw_comments($tpl_source)
{
    return preg_replace("/<!--#.*-->/U","",$tpl_source);
}

// register the prefilter
$smarty->register_prefilter("remove_dw_comments");
$smarty->display("index.tpl");
?>

{* Smarty template index.tpl *}
<!--# this line will get removed by the prefilter -->