Usage of the smarty advanced features filter _ php instance

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.