This article describes how to use the smarty advanced filter. It analyzes the usage skills of the smarty filter in combination with examples, which has some reference value, for more information about how to use the smarty advanced filter, see the examples in this article. We will share this with you for your reference. The details are 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 comments from the original tpl file, make it not displayed in the compiled file} // register the pre-filter $ smarty-> register_prefilter ("remove_dw_comments"); $ smarty-> display ("test1.tpl ");
Test1.tpl
Use of filters
Annotation format
In this case, the comment is filtered out in the compiled file.
2. Post Filter
Function add_header_comment ($ tpl_source, & $ smarty) {return "<? Php echo \"
\ N \ "?> \ N ". $ tpl_source; // Add header annotation} // after registration filter $ smarty-> register_postfilter ("add_header_comment"); $ smarty-> display ('test2. tpl ');
Template file:
Test2.tpl
Comments are generated in the header:
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");
I hope this article will help you design PHP programs based on the smarty template.