6.1 模板过滤器
6.1.1 预过滤器pre
预滤器用来在编译之前直接处理模板源文件。预滤器函数的第一个参数是模板源文件,该文件可能被其他一些预滤器修正过。此预滤器插件将返回修正过的源文件。
请记住此源文件仅用来编译,它不会在任何地方被保存。
有两种方式:
第一种是临时注册
$smarty->register->preFilter(‘mypre’); #注册一个预编译器函数
$smarty->unregister->preFilter (‘mypre’); #删除一个预编译器函数
function mypre($tpl_source, &$smarty)
#在PHP文件中定义一个预编译器函数,参数格式固定,不可变,返回编译数据
{
return “mypre<br />”.$tpl_source;
}
第二种是载入组件
在smarty程序目录中有libs/plugins目录,在这里我们可以创建自己的过滤器组件。
创建php文件,prefilter.函数名.php(本例中是prefilter.mypre.php) #也可以用其他组件的php文件
function smarty_prefilter_mypre($source, $smarty)
#在smarty组件文件中定义一个预编译器函数组件,函数名称和参数严格不变
{
return “mypre_plus<br />”.$source;
}
$smarty->autoload_filters = array(‘pre’=>array(‘mypre’)); #在php文件中调用组件过滤器
$smarty->autoload_filters = array() #取消调入的组件过滤器
过滤器可以载入多个,
$smarty->register->preFilter(‘mypre’); #第一种方法,就是罗列一下,在上面先执行谁
$smarty->register->preFilter(‘mypre1′);
$smarty->autoload_filters = array(‘pre’=>array(‘mypre’,’mypre1′)); #第二种方法,就是建立数组就行,在前面的先执行。
6.1.2 后过滤器post
后滤器用来在编译之后直接处理模板的编译输出(PHP代码),
但须在编译之后的模板被保存到文件系统之前就进行操作。
预滤器函数的第一个参数是编译之后的模板代码,该代码可能被其他一些后滤器修正过。
此后滤器插件将返回修正过的代码文件。
有两种方式:
第一种是临时注册
$smarty->register->postFilter(‘mypre’); #注册一个后编译器函数
$smarty->unregister->postFilter (‘mypre’); #删除一个后编译器函数
function mypost($tpl_source, &$smarty) #在php文件中定义一个后编译器函数,参数格式固定,不可变,返回编译数据
{
return “mypost<br />”.$tpl_source;
}
第二种是载入组件
在smarty程序目录中有libs/plugins目录,在这里我们可以创建自己的过滤器组件。
创建php文件,postfilter.函数名.php(本例中是postfilter.mypost.php) #也可以用其他组件的php文件
function smarty_postfilter_mypost($source, $smarty)
#在smarty组件文件中定义一个预编译器函数组件,函数名称和参数严格不变
{
return “mypost_plus<br />”.$source;
}
$smarty->autoload_filters = array(‘post’=>array(‘mypost’)); #在php文件中调用组件过滤器
$smarty->autoload_filters = array(‘post’=>array()) #取消调入的组件过滤器
过滤器可以载入多个,
$smarty->register->postFilter(‘mypost’); #第一种方法,就是罗列一下,在上面先执行谁
$smarty->register->postFilter(‘mypost1′);
$smarty->autoload_filters = array(‘post’=>array(‘mypost’,’mypost1′));
#第二种方法,就是建立数组就行,在前面的先执行。
6.1.3 输出过滤器output
输出过滤器插件的作用是,在装载并执行完一个模板之后显示模板之前,操作该模板的输出。
pre和post都会将过滤器加工后的结果硬编码写入编译文件,
也就意味着pre和post始终会被缓存(即使php不缓存)。
output不会,将不会写入编译文件以及之后的缓存文件,所以他是不被缓存的。
pre和post对于模板中调用的模板,比如include file,均需执行一遍。
而output只对编译后的文件执行一次(也就是总共执行一次)
有两种方式:
第一种是临时注册
$smarty->register->outputFilter(‘myoutput’); #注册一个后编译器函数
$smarty->unregister->outputFilter (‘mypre’); #删除一个后编译器函数
function myoutput($tpl_source, &$smarty)
#在php文件中定义一个后编译器函数,参数格式固定,不可变,返回编译数据
{
return “myoutput<br />”.$tpl_source;
}
第二种是载入组件
在smarty程序目录中有libs/plugins目录,在这里我们可以创建自己的过滤器组件。
创建php文件,outputfilter.函数名.php(本例中是outputfilter.myoutput.php) #也可以用其他组件的php文件
function smarty_outputfilter_myoutput($source, $smarty)
#在smarty组件文件中定义一个预编译器函数组件,函数名称和参数严格不变
{
return “myoutput_plus<br />”.$source;
}
$smarty->autoload_filters = array(‘output’=>array(‘myoutput’)); #在php文件中调用组件过滤器
$smarty->autoload_filters['output'] = array() #取消调入的组件过滤器
过滤器可以载入多个,
$smarty->register->outputFilter(‘myoutput’); #第一种方法,就是罗列一下,在上面先执行谁
$smarty->register->outputFilter(‘myoutput1′);
$smarty->autoload_filters = array(‘output’=>array(‘myoutput’,’myoutput1′));
#第二种方法,就是建立数组就行,在前面的先执行。
6.2 错误和异常
6.2.1 触发错误 trigger_error
php中也有trigger_error函数
Void trigger_error(string error_msg, [int level]) #int level就是错误级别,也可以用常量代替,E_USER_NOTICE, E_USER_WARNING等。set_error_handler()函数进行捕获处理。
$smarty->trigger_error($error_msg, $error_type = E_USER_WARNING)
#格式与PHP中的同名函数一样,只能抛出$smarty的异常
6.2.2 错误处理函数
$smarty->setExceptionHandler(handlerfuncname)
# 设置异常处理的函数,只可以处理有smarty引发的错误,
不能解决trigger_error。set_error_handler()函数只能解决php的,而不能解决smarty抛出的错误。
function handlerfuncname($errstr)
{
echo $errstr;
}
6.3 数据对象
3.0推出一个新的数据对象。可以从$smarty中创建一个或者多个数据对象,集中赋值,
然后有选择性选取某个或者多个数据对象,生成模板。
$smarty->createData($parent = null) #创建数据对象,可以选择父对象,子对象将继承父对象的赋值。
例子:
$mydata = $smarty->createData(); #创建一个数据对象,没有父对象
$mydata1 = $smarty->createData($mydata); #创建一个数据对象,父对象是$mydata
$mydata->assignByRef(‘assign_obj’,&$han);
$mydata->assign(‘name’,’abcd’);
$mydata1-> assign(‘name’,’bcds’); #mydata1自动继承了mydata的数据,如果相同,则新值覆盖旧值
$smarty->display(‘templates/t4.tpl’,null,null,$mydata1);
#显示模板,最后一个参数必须有,指定模板使用哪个数据对象。也可以使用模板对象调用,参看6.4
6.4 模板对象
3.0同时推出一个新的模板对象。可以从$smarty中创建一个或者多个模板对象,
模板对象可以自己分配变量,同时可以调用数据对象(父对象)来生成文件。
$smarty->createTemplate($template, $cache_id = null, $compile_id = null, $parent = null)
#创建模板对象,可以选择父对象,编译id和缓存id。
例子:
$mytemplate = $smarty->createTemplate(‘templates/t4.tpl’); #创建一个模板对象,没有父对象,默认为$smarty
$mytemplate1 = $smarty->createTemplate(‘templates/t4.tpl’,null,null,$mydata); #创建一个模板对象,父对象为mydata,mydata中的数据自动加载进来。
$mytemplate->assignByRef(‘assign_obj’,&$han); #定义变量
$mytemplate->assign(‘name’,’abcd’);
$mytemplate->display(); #生成文件。
如果一个模板是通过include方式调用的,则子模板的父对象将指向引用它的模板对象。
所有当前模板变量和父对象的模板对象都是可以获取的,但是如果是通过{assign}或者{$foo=…}
这样的方法创建或者修改变量则它的作用域将只停留在当前模板对象。
6.5 模板继承
模板继承,可以在模板中写{block} … {/block}快,并且这些块可以在子模板中进行覆盖。例子:
Parent.tpl
—————————————————–
<html>
<body>
{block name=’top’} Parent.tpl的头部<br />{/block}<hr />
{block name=’middle’} Parent.tpl的中部<br />{/block}<hr />
{block name=’buttom’} Parent.tpl的尾部<br />{/block}
</body>
</html>
child.tpl
{extends file=’parent.tpl’} #继承父模板
{block name=’top’}{$smarty.block.parent}{“`$smarty.template`的头部”} {/block}
#child模板更新了top块。其他按照默认继承。
可以通过extends标签来指定被继承的模板,并在子模板中通过重写父模板的同名block块,达到覆盖的目的。
同时,可以通过{$smarty.block.parent}获取到父block的内容。
在子模板中,所有在{block} … {/block}之外的内容都将被忽略,这种继承支持多文件,多重继承,
意味着可以无限的继承下去。还可通过{block}的append和prepend属性来插入父模板结构中
6.6 资源相关内容(未写)
6.7 动态注册组件
注册可以理解为是动态的注册组件,与写死到文件中的组件类似,参考6.8
6.7.1 注册对象
SMARTY允许通过模板访问PHP对象。有两种方式来访问它们。
一种是注册对象到模板,然后通过类似于用户自定义函数的形式来访问它。
$smarty->register->templateObject($object_name, $object_impl, $allowed = array(),
$smarty_args = true, $block_methods = array())
#向模板注册一个对象,allowed是允许接受的方法。
$smarty->register->templateObject(‘reg_obj’,$han,array(‘show’,’show1′));
{reg_obj->show var=2 var1=3} #在模板中访问,注意!接受的参数是数组,跟insert类似
$smarty->unregister->templateObject($object_name) #注销对象
第一种方法有一个很好的模板语法,同时它作为一个注册对象被限制为几个固定的方法和目标,
这样是比较安全的,但是他只能够调用对象方法,而且不支持数据对象和模板对象。
另一种方法给模板分配对象,然后通过访问其它赋值变量类似的方法进行访问。
$mysmarty->assignByRef(‘assign_obj’,&$han); #建议使用引用分配,对象一般都很大,节省内存
{$assign_obj->方法或者属性} #在模板中访问
这种方法可以调用对象的属性。而且可以用数据对象和模板对象注册
6.7.2 注册块
$smarty->register->block($block_tag, $block_impl, $cacheable = true, $cache_attr = array())
用来动态注册/定义块函数插件。前两个参数指定块函数名称和执行函数的名称。
执行函数的名称格式可以是一个包含函数名称的字符串;
也可以是一个array(&$object, $method)数组形式,其中&$object是一个对象的引用,而$method是它的一个方法;
还可以是一个array(&$ class, $method)数组形式,其中$class是一个类的名称,$method是类中的一个方法。
$cacheable,如果启用页面缓存,块级函数是否缓存,默认是true。
$cacheattr,如果$cacheale为false,也就是块级函数不缓存,可以设置块级函数中的部分属性缓存,已数组定义。
例子:
$hanobj = new han();
$smarty->setcaching(true);
$smarty->register->block(‘hhh’, array(&$hanobj,’myf’),false,array(‘fn’)); #动态注册块
class han{
。。。。。。
public function myf($params,$content,&$smarty,&$repeat){ #定义块引用的函数
return “这是”.$params['fn'].$params['un'].”注释说明!<br />\r\n”.$content;
}
}
{hhh fn=$name un=$name1} #在模板中调用,块级内容没有被缓存,属性fn被缓存
aaaaaaa
{time()}
{/hhh}
$smarty->unregister->block($block_tag) #注销块
6.7.3 注册函数
$smarty->register->compilerFunction($compiler_tag, $compiler_impl, $cacheable = true)
#注册编译函数,编译函数不能指定缓存属性
$smarty->register->templateFunction($function_tag, $function_impl, $cacheable = true, $cache_attr = array())
#注册模板函数,3.0后可以直接在模板中定义函数了,不需要注册动态注册模板函数插件,前两个参数是模板函数名称和执行函数名称。
执行函数的格式可以是一个包含函数名称的字符串;也可以是一个array(&$object, $method)数组形式,其中&$object是一个对象的引用,而$method是它的一个方法;
还可以是一个array(&$ class, $method)数组形式,其中$class是一个类的名称,$method是类中的一个方法。
例子:
$hanobj = new han();
$smarty->setcaching(true);
$smarty->register->templateFunction(‘hhh’, array(&$hanobj,’myf’),false,array(‘fn’));
class han{
…………….
public function myf($params,&$smarty,&$repeat){ #注册的函数只有3个参数
return time().”这是”.$params['fn'].$params['un'].”注释说明!<br />\r\n”;
}
}
{hhh fn=$name un=$name1} #在模板中调用,函数没有被缓存,属性fn被缓存
$smarty->unregister->compilerFunction($compiler_tag) #注销注册函数
$smarty->unregister->templateFunction($function_tag)
6.7.4 注册变量调节器
$smarty->register->modifier($modifier_name, $modifier_impl)
动态注册调节器函数插件,前两个参数是模板调节器名称和执行函数名称。执行函数的格式可以是一个包含函数名称的字符串;
也可以是一个array(&$object, $method)数组形式,其中&$object是一个对象的引用,而$method是它的一个方法;还可以是一个array(&$ class, $method)数组形式,
其中$class是一个类的名称,$method是类中的一个方法。
例子:
$hanobj = new han();
$smarty->setcaching(true);
$smarty->register->modifier(‘hhh’,array(&$hanobj,’myf’)); #注册调节器
class han{
。。。。。。
public function myf($string) {
return time().$string;
}
}
{$name|hhh nocache=true} # 在模板中调用,nocache为true,表示该变量不缓存
$smarty->unregister->modifier($modifier) #注销变量调节器
6.8 组件相关内容
6.8.1 建立过滤器组件
请参考6.1.1 预过滤器pre
请参考6.1.2 后过滤器 post
请参考6.13 输出过滤器 output
6.8.2 建立块组件
在libs/plugins中创建块组件文件block.块名.php
function smarty_block_hhhh($params,$content,&$smarty,&$repeat) #函数名称格式要固定
{
return “这是”.$params['fn'].$params['un'].”注释说明!<br />\r\n”.$content;
}
{hhhh fn=$name un=$name1} #在模板中调用
aaaaaaa
{time()}
{/hhhh}
与动态注册相比,不能规定块缓存
6.8.3 建立函数组件
在libs/plugins中创建函数组件文件function.函数名.php
function smarty_function_hhhh($params,&$smarty,&$repeat){ #在组建文件中建立函数
return time().”这是”.$params['fn'].$params['un'].”注释说明!<br />\r\n”;
}
{nocache}
{hhhh fn=$name un=$name1} #在模板中调用,如果想不缓存,可以用{nocache}括住
{/nocache}
6.8.4 建立变量调节器组件
在libs/plugins中创建调节器组件文件modifier.调节器名.php
function smarty_modifier_hhhh($string) #在组建文件中建立调节器函数
{
return time().$string;
}
{$name|hhhh nocache=true} #在模板中调用
6.8.5 建立insert函数组件
在libs/plugins中创建调节器组件文件insert.函数名.php
function smarty_insert_hhhh($params,&$smarty){ #在组件文件中建立insert函数
return time().”这是”.$params['fn'].”|”.$params['un'].”注释说明!<br />\r\n”;
}
{insert name=”hhhh” fn=$name un=$name1} #在模板中调用函数内部不缓存,但是传递的参数会缓存。