最近在研究smarty相关的一些东西,发现smarty的插件好想很厉害的样子,为我等程序狗程序开放提供了极大的方便,真的是棒棒的~~

so,在此记录学习一下。

插件类型

functions 函数插件
modifiers 修饰插件
block functions 区块函数插件
compiler functions 编译函数插件
prefilters 预滤器插件
postfilters 补滤器插件
outputfilters 输出过滤插件
resources 资源插件
inserts 嵌入插件

命名规则

smarty_type_name()

例如:smarty_modifier_date_format()

上面的date_format即为插件名,这是一个修饰类型的插件,在模板中调用方法如下:

{$yesterday|date_format:”%H:%M:%S”}

编写插件

一般来讲,modifier和function是最常用的两种插件,写个function类型的插件作为示例:

文件名规则:function_example.php

定义:function smarty_function_example($params, &$smarty) {…}

在模板中调用:{example}

如果某个插件依赖其它插件内的某些功能(例如某些插件功能捆绑于Smarty内),那么可以通过如下方法装载必须的插件:

require_once $smarty->_get_plugin_filepath(‘function’, ‘html_options’);