php 高级特性,smarty高级特性之过滤器的使用方法

这篇文章主要介绍了smarty高级特性之过滤器的使用方法,结合实例形式分析了smarty过滤器的相关使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例分析了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程序设计有所帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值