Smarty Advanced Features filter Usage _php instance

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

  • 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.