• 技术文章 >后端开发 >php教程

    smarty高级特性之过滤器的使用方法_php实例

    2016-06-07 17:10:18原创383

    本文实例分析了smarty高级特性之过滤器的使用方法。分享给大家供大家参考,具体如下:

    高级特性中过滤器的使用

    1、预过滤器

    function remove_dw_comments($tpl_source, &$smarty)
    {
     return preg_replace("//U","",$tpl_source);
     //去除原tpl文件中的注释,使其在编译后的文件中不显示
    }
    //注册预过滤器
    $smarty->register_prefilter("remove_dw_comments");
    $smarty->display("test1.tpl");
    

    test1.tpl

    与过滤器的使用


    注释的格式
    这样的话,注释在编译后的文件中被过滤掉

    2、后过滤器

    function add_header_comment($tpl_source, &$smarty)
    {
     return "<?php echo \"\n\" ?>\n".$tpl_source;
     //添加头部注释
    }
    //注册后过滤器
    $smarty->register_postfilter("add_header_comment");
    $smarty->display('test2.tpl');
    
    

    模板文件:

    test2.tpl

    头部会产生注释:

    3、输出滤镜

    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");
    
    

    希望本文所述对大家基于smarty模板的PHP程序设计有所帮助。

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    上一篇:yii2.0实现验证用户名与邮箱功能_php实例 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • php实现通过JSON RPC与go通讯(附代码)• 浅析怎么使用PHP做异步爬取数据• PHP8.3要有新函数了!(json_validate函数说明)• 设计API接口时,要注意这些地方!• PHP网站常见一些安全漏洞及防御方法
    1/1

    PHP中文网