Loading...
 

SmartyFilters

Since Tiki uses the Smarty templating engine to display templates you can use Smarty filters to modify the way in which templates are processed.

A template is displayed following these steps:

A template source (.tpl file) containing HTML markup and smarty syntax is compiled into a .php file

Then the .php file is executed to display the template.

A filter is a PHP function that receives a string and returns another string. Example:

function foo($string) {
return - .$string. - ;
}

There are three filters that you can use:

  • Prefilters
  • Postfilters
  • Outputfilters

Prefilters


Prefilters are execited before templates are compiled, so they receive a string containing a template source and return another string with the modified template source. Prefilters are used to process templates before they are compiled. For example converting your ad-hoc syntax to smarty syntax. In tiki we use a prefilter to translate all the strings between {tr}{/tr} before compiling templates so we translate most of the strings when templates are compiled and not every time the template is displayed (the strings that can't be translated at this time and that contain a smarty variable will be looked for translation again after the variable substitution).

Postfilters


Postfilters are used after templates are compiled. So they receive a .php source and return a modified .php source. Postfilters are used to inject php code into compiled templates.

Outputfilters


Output filters are executed just before sending the parsed template to the browser, normally outputfilters receive HTML data and return HTML data, can be used for example to compress HTML pages before sending them to the browser.

How to add a filter:


Write a file called filtertype.name.php in the Smarty/plugins directory containing a function named smarty_filtertype_name. Example:

File: prefilter.foo.php
Function : smarty_prefilter_foo

Then you have to activate (register) your filter using the load_filter Smarty method in the setup.php file.

Find the line:

$smarty->load_filter('pre','tr');

And add your own filter the load_filter syntax is

load_filter(String filtertype, String filtername);

Where filtertype can be pre , pos or output


Page last modified on Sunday 14 December 2003 21:35:02 GMT-0000