Get Smarty

Donate

Paypal

Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Sites Using Smarty

Advertisement

前置/后置过滤器

前置过滤器和后置过滤器在概念上非常相似,不同点仅在于执行的时机。

string smarty_prefilter_name( $source,  
  $template);  
string $source;
object $template;
 

前置过滤器是在模板源码被编译前的时刻执行。 第一个参数是模板源码,源码可能已被其他过滤器处理过的。 前置过滤器插件将处理并返回过滤后的源码。 注意这些返回的源码不会被保存,它们仅提供给编译使用。

string smarty_postfilter_name( $compiled,  
  $template);  
string $compiled;
object $template;
 

后置过滤器会在编译器已经把模板编译完成,但未保存到文件系统的时候执行。 第一个参数是已编译的代码,代码可能被其他过滤器处理过的。 后置过滤器将进行处理并返回处理后的代码。

Example 18.7. 前置过滤器


<?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     prefilter.pre01.php
 * Type:     prefilter
 * Name:     pre01
 * Purpose:  转换HTML标签为小写
 * -------------------------------------------------------------
 */
 function smarty_prefilter_pre01($source, Smarty_Internal_Template $template)
 {
     return preg_replace('!<(\w+)[^>]+>!e', 'strtolower("$1")', $source);
 }
?>

     

Example 18.8. 后置过滤器


<?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     postfilter.post01.php
 * Type:     postfilter
 * Name:     post01
 * Purpose:  列出当前模板的变量
 * -------------------------------------------------------------
 */
 function smarty_postfilter_post01($compiled, Smarty_Internal_Template $template)
 {
     $compiled = "<pre>\n<?php print_r(\$template->getTemplateVars()); ?>\n</pre>" . $compiled;
     return $compiled;
 }
?>

     

参见: registerFilter() unregisterFilter().

Comments
No comments for this page.

Advertisement

Sponsors [info]

Sponsors