Creating Filters

There are three main ways to create filters and make them available on Smarty templates:

  • Register a filter plug-in at run time and point Smarty to a PHP function callback from within the template script by using one of the register_*filter functions.
  • Tell Smarty to automatically load a filter from the plugin directory with the load_filter function.
  • Manually set the autoload_filters Smarty property to make it load the appropriate filters automatically for you.

Let’s go over these different ways now. To register a filter plug-in at run time, you need to use one of the following Smarty functions:

Type

Method signature to register a plug-in

Prefilters

void register_prefilter(mixed impl)

Postfilters

void register_postfilter(mixed impl)

Output filters

void register_outputfilter(mixed impl)

Please note that the parameter called impl above refers to the function or method name that Smarty will call, and it can be one of the following:

  • A string value holding the function name
  • An array of the form array($object, ‘method_name’)
  • An array of the form array(‘class_name’, ‘method_name’)

When actually writing the PHP code for the filter plug-ins, follow these rules for the function arguments:

Type

Function signature when writing the filter

Prefilters

string function_name(string source, object smarty)

Postfilters

string function_name(string source, object smarty)

Output filters

string function_name(string output, object smarty)

What this means is that for filter plug-ins, Smarty will pass the template source or template output as the first argument and the special Smarty object as the second argument to your PHP function, and expect you to return the modified source or output from that function.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.136.165