Question

I am rather confused about how to use post / pre filters with smarty.

What I need to do, is to search and replace certain elements in the page before it is displayed. I would rather like to do this right before $smarty->display is called. That means, before it gets saved to disk, but after it has been compiled (so, postfilter).

Example postfilter function:

function smarty_postfilter_replace($tpl_output, $search, $replace)
{
    return str_replace($search, $replace, $tpl_output);
}

How do I apply this right before smarty output. I have tried after looking at the Smarty documentation ( http://www.smarty.net/docsv2/de/advanced.features.postfilters.tpl ) and some other examples, but none seems to be working.

can someone give me an example code on how to do this, and how to pass parameters to the filter ($search, $replace).

Thanks

Was it helpful?

Solution

The post-filter is probably the most misunderstood filter. While the pre-filter runs on the uncompiled template source code, and the output-filter runs on the evaluated output (y'know, the generated HTML), the post-filter is run as part of the compiler - it's fed the PHP produced by the compiler. I'm not sure what anyone would do with this. In any case, it's not what you're looking for.

Use the output-filter to replace your content. If you're using caching, it is run before writing to cache, if you don't have any non-caching elements. If you have non-caching elements (like {nocache} or variables with nocache flag), the output filter is run after the cache has been evaluated (pretty much on every request).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top