smarty的插件功能是smarty模板的精华

简介:

一,smarty插件介绍

smarty的插件放在/smarty/libs/plugins下面,它为程序的开发 提供了很大的方便,例如:{$yesterday|date_format:"%H:%M:%S"}smarty自带的日期格式化插件,对变 量$yesterday进行格式化。在我们的php文件中,并不需要对date_format进行处理,我们只要拿来用就好了。

二,smarty插件命名规则

1,插件文件名命名规则

 

type name .php

type有以下几种

  1. function
  2. modifier
  3. block
  4. compiler
  5. prefilter
  6. postfilter
  7. outputfilter
  8. resource
  9. insert

例如:modifier .date_format .php这个就是smarty自带的日期插件的文件名

2,插件文件里面的函数命名规则

smarty_type _name ()

例如:smarty_modifier _date_format

上面的紫色字对应的是插件类型,桔黄色字对应的是插件名称

三,添加自定义插件功能

个人觉得modifier 和function 这二种类型的插件最有用,也是最常用的。所以下面我以这二个类型来举例子

1,添加modifier插件

a ),/smarty/libs/plugins下面建个文件modifier.reverse.php

Java代码   收藏代码
  1. <?php  
  2. function smarty_modifier_reverse($string)  
  3. {  
  4.  if(is_string($string)){  
  5.   return strrev($string);  
  6.  }else{  
  7.   return false;  
  8.  }  
  9. }  
  10. ?>  

 b),在调用模块的文件文件里加上

Java代码   收藏代码
  1. $this->tpl->assign("test""123456789");  

 c),在模块文件文件中加入

Java代码   收藏代码
  1. <div>reverse == {$test|reverse}</div>     

上面的这个例子是把一个字符串进行反转,结果是:987654321

2,添加function插件

a ),/smarty/libs/plugins下面建个文件function.html_lis.php

Java代码   收藏代码
  1. function smarty_function_html_lis($params, &$smarty)  
  2. {  
  3.     require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');  
  4.     $class = 'li_style';  
  5.     $options = null;  
  6.     $separator = '';  
  7.     $js = '';  
  8.     $labels =true;  
  9.     $output = null;  
  10.     $extra = '';  
  11.     foreach($params as $_key => $_val) {  
  12.         switch($_key) {  
  13.             case 'class':  
  14.             case 'separator':  
  15.                 $$_key = $_val;  
  16.                 break;  
  17.   
  18.             case 'labels':  
  19.                 $$_key = (bool)$_val;  
  20.                 break;  
  21.   
  22.             case 'js':  
  23.                 $$_key = $_val;  
  24.                 break;  
  25.   
  26.             case 'options':  
  27.                 $$_key = (array)$_val;  
  28.                 break;  
  29.   
  30.             case 'output':  
  31.                 $$_key = array_values((array)$_val);  
  32.                 break;  
  33.   
  34.             case 'lis':  
  35.                 $smarty->trigger_error('html_lis: the use of the "lis" attribute is deprecated, use "options" instead', E_USER_WARNING);  
  36.                 $options = (array)$_val;  
  37.                 break;  
  38.   
  39.             default:  
  40.                 if(!is_array($_val)) {  
  41.                     $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';  
  42.                 } else {  
  43.                     $smarty->trigger_error("html_lis: extra attribute '$_key' cannot be an array", E_USER_NOTICE);  
  44.                 }  
  45.                 break;  
  46.         }  
  47.     }  
  48.   
  49.     if (!isset($options) && !isset($values))  
  50.         return ''/* raise error here? */  
  51.   
  52.     $_html_result = array();  
  53.   
  54.     if (isset($options)) {  
  55.   
  56.         foreach ($options as $_key=>$_val)  
  57.             $_html_result[] = smarty_function_html_lis_output($class, $_key, $_val, $extra, $separator, $labels, $js);  
  58.   
  59.     } else {  
  60.         foreach ($values as $_i=>$_key) {  
  61.             $_val = isset($output[$_i]) ? $output[$_i] : '';  
  62.             $_html_result[] = smarty_function_html_lis_output($class, $_key, $_val, $extra, $separator, $labels, $js);  
  63.         }  
  64.   
  65.     }  
  66.   
  67.     if(!empty($params['assign'])) {  
  68.         $smarty->assign($params['assign'], "<ul>".$_html_result."</ul>");  
  69.     } else {  
  70.   
  71.         return "<ul>".implode("\n",$_html_result)."</ul>";  
  72.     }  
  73. }  
  74.   
  75. function smarty_function_html_lis_output($class, $value, $output, $extra, $separator, $labels, $js) {  
  76.     $_output = '';  
  77.     if ($labels) $_output .= '';  
  78.     $_output .= '<li tip="'  
  79.         . smarty_function_escape_special_chars($value) . '"';  
  80.  if($js) $_output .= $js  ;  
  81.     $_output .= $extra . ' />' . $output;  
  82.     if ($labels) $_output .= '';  
  83.     $_output .=  $separator;  
  84.   
  85.     return $_output;  
  86. }  
  87.   
  88. ?>  

  b),在调用模块的文件文件里加上

Java代码   收藏代码
  1. $this->tpl->assign('cust_lis', array(  
  2.    "china" => '中国',  
  3.    "shanghai" => '上海',  
  4.    "heifei" => '合肥',  
  5.    "luan" => '六安'));  
  6. $this->tpl->assign("onclick""onclick=sep()");   

 c),在模块文件文件中加入

Java代码   收藏代码
  1. <div>  
  2. {html_lis  options=$cust_lis js=$onclick}  
  3. </div>  

 d),输入结果为

Java代码   收藏代码
  1. <ul><li class="li_style" tip="china" onclick=sep() />中国  
  2. <li class="li_style" tip="shanghai" onclick=sep() />上海  
  3. <li class="li_style" tip="heifei" onclick=sep() />合肥  
  4. <li class="li_style" tip="luan" onclick=sep() />六安</ul>  

 上面的例子是生成ul标签的一个smarty插件,把checkbox拿过来改一改,而成的。

相关文章
symfony框架Twig模板语言的使用
symfony框架Twig模板语言的使用
116 0
symfony框架Twig模板语言的使用
|
PHP
在smarty模板中使用PHP函数的方法
sample1 复制代码 代码如下: 那如果使用像iconv这样的有三个参数的函数该怎么写呢?如果写成: sample 2 复制代码 代码如下: 一执行就会发现显示error信息。 因此研究一下就会发现,起始在smarty模板页的套用函数用法中,以smaple 1来说,trim的前面$Row->colname其实就是trim的第一个参数,中间用|这个符号串接; 那假设要使用像iconv有三个参数的函数的话,就要写成: sample 3 复制代码 代码如下: 也就是 函数第一个参数|函数:第二个参数:第三个参数。
1182 0
|
编解码 JavaScript 前端开发
|
JavaScript PHP
ecshop模板切换到smarty3.1.30
ecshop使用的是smarty的阉割版 如果使用smarty3来替换掉ecshop的原版呢,有一些不兼容的地方一个个地来排除,第一个  {insert_scripts files='jquery.json.js,transport.js'} 在libs/plugins文件夹中新建文件function.insert_scripts.php,加入以下代码 function
1178 0
|
Web App开发 JavaScript .NET
EJS 模板快速入门
Node 开源模板的选择很多,但推荐像我这样的老人去用 EJS,有 Classic ASP/PHP/JSP 的经验用起 EJS 来的确可以很自然,也就是说,你能够在 块中安排 JavaScript 代码,利用最传统的方式 (另外 Function ejs.
987 0
|
PHP 索引
ecshop的smarty库还原成smarty原生库方法
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私募机构九鼎控股打造,九鼎投资是在全国股份转让系统挂牌的公众公司,股票代码为430719,为“中国PE第一股”,市值超1000亿元。
932 0