PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Prefilters/Postfilters

Prefilter and postfilter plugins are very similar in concept; where they differ is in the execution -- more precisely the time of their execution.

string smarty_prefilter_name( $source,  
  $template);  
string $source;
object $template;
 

Prefilters are used to process the source of the template immediately before compilation. The first parameter to the prefilter function is the template source, possibly modified by some other prefilters. The plugin is supposed to return the modified source. Note that this source is not saved anywhere, it is only used for compilation.

string smarty_postfilter_name( $compiled,  
  $template);  
string $compiled;
object $template;
 

Postfilters are used to process the compiled output of the template (the PHP code) immediately after the compilation is done but before the compiled template is saved to the filesystem. The first parameter to the postfilter function is the compiled template code, possibly modified by other postfilters. The plugin is supposed to return the modified version of this code.

Example 260. prefilter plugin



<?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     prefilter.pre01.php
 * Type:     prefilter
 * Name:     pre01
 * Purpose:  Convert html tags to be lowercase.
 * -------------------------------------------------------------
 */
 
function smarty_prefilter_pre01($sourceSmarty_Internal_Template $template)
 {
     return 
preg_replace('!<(\w+)[^>]+>!e''strtolower("$1")'$source);
 }
?>

     

Example 261. postfilter plugin



<?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     postfilter.post01.php
 * Type:     postfilter
 * Name:     post01
 * Purpose:  Output code that lists all current template vars.
 * -------------------------------------------------------------
 */
 
function smarty_postfilter_post01($compiledSmarty_Internal_Template $template)
 {
     
$compiled "<pre>\n<?php print_r(\$template->getTemplateVars()); ?>\n</pre>" $compiled;
     return 
$compiled;
 }
?>

     

See also registerFilter() and unregisterFilter().

Smarty Template Engine