Filters are simply ordinary functions you can tell SimpleTemplate to filter your variables through before embedding them into a template's output.
SiteTemplate's default is to pass all variables through htmlentities_compat() as defined in saf/lib/Functions.php, which helps prevent accidental Cross-site scripting (XSS) attacks on your apps. This can be turned off for a variable via the 'none' filter name.
Here's a custom filter example:
inc/app/myapp/lib/Filters.php:
<?php
function myapp_filter_uppercase ($str) {
return strtoupper ($str);
}
?>
Note how I included my app name and the word 'filter' in my function name. This is recommended to help distinguish between functions defined in different apps, as well as the purpose of your function.
inc/app/myapp/boxes/index/index.php:
<?php
// import the filter package
loader_import ('myapp.Filters');
echo template_simple (
'index.spt',
array (
'test_var' => 'Test Variable',
)
);
?>
inc/app/myapp/html/index.spt:
<p>
Testing my new filter:
{filter myapp_filter_uppercase}{test_var}{end filter}
</p>
<p>
Of course, I could simply have used strtoupper:
{filter strtoupper}{test_var}{end filter}
</p>
Here are some additional filter tricks:
1. Turning off filtering:
{filter none}{body}{end filter}
2. One filter call can apply to more than one variable:
{filter rawurlencode}
<a href="/index/myapp-somebox-action?var1={var1}&var2={var2}&var3={var3}">Click Me!</a>
{end filter}
Created on April 4, 2005 2:05 PM by lux
Linked from: Templates