In this paper, the use of filters for Smarty advanced Features is analyzed. Share to everyone for your reference, as follows:
Use of filters in advanced features
1. Pre-Filter
function remove_dw_comments ($tpl _source, & $smarty) {return preg_replace ("/
U", "", $tpl _source);// Remove the annotations in the original TPL file so that they do not display the}//registration pre-filter $smarty->register_prefilter ("Remove_dw_comments") in the compiled file; $smarty->display (" Test1.tpl ");
Test1.tpl
With the use of filters
Formatting of annotations
In this case, the comments are filtered out in the compiled file.
2, after the filter
function Add_header_comment ($tpl _source, & $smarty) {return "<?php echo \"
\n\ "? >\n". $tpl _source; /Add Header comment}//registered filter $smarty->register_postfilter ("Add_header_comment"); $smarty->display (' Test2.tpl ');
Template file:
Test2.tpl
The head will produce comments:
3. Output filter
function Protect_email ($tpl _output, & $smarty) { $tpl _output = preg_replace ('! \s+) @ ([A-za-z0-9\.\-]+\. ( [A-za-z] {2,3}| [0-9] {1,3}))! ', ' $1%40$2 ', $tpl _output); return $TPL _output;} Register the Outputfilter$smarty->register_outputfilter ("Protect_email"); $smarty->display ("Index.tpl");} $smarty->register_outputfilter ("Protect_email"); $smarty->display ("Index.tpl");
It is hoped that this article will be helpful to everyone based on smarty template PHP program design.
The above describes the Smarty advanced features of the use of filters, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.