在smarty中自定义函数

       首先smarty中的自定义函数可以是用户可以手动修改的,它分为:

一种可以配合注册函数(register_function())使用

一种是直接写成插件放入plugins下

调用这些函数时都在模板里调用 写成该种格式

<{函数名  参数1=“ ”  参数2=“ ”  。。。}>


1、 smarty函数:

a)        实现对smarty功能的扩展

b)       内建函数:smarty自带  不能修改

  内建函数只能在php文件中调用;

 

c)        自定义函数:可以修改 

1、自定义函数可以在php文件中使用(配合注册函数),

2、也可以在模板文件中使用(需要将函数定义为插件放到plugins文件夹中)

 

在模板文件中调用

<{title  num="10"  content="aaa"  size="10"   color="green"}>

 

保存在plugins目录下

语法格式:类似于html标记的语法

<{…..}>

       例如:

              Smarty自定义函数:assign();

              函数名称:assign

              参数:    var    声明变量名称

                            Value  给变量赋值

              如:<{assign  var=”title” value=”这是title”}>        

 $tpl->assign(“title”,”this  is title”);

             

              属性无顺序要求

              <{config_load  file=”*.conf” section=”one”}>

 

 

 

在php文件中创建自定义函数:

 

例如:<{title  num=”10”  content=”aaa”  size=”10”   color=”green”}>

 

注册函数:

       第一步:声明函数:

              Functionfun1

 

模板文件中

<{title num="10" content="aaa" size="10"  color="green"}>

       第二步:注册函数

              $tpl->register_function();smarty中的函数

注册函数register_function("模板中自定义函数名","php中自定义函数名")

              $tpl->register_function("title","fun1");

       functionfun1($args){

              $a="";

              for($i=0;$i<=$args["num"]; $i++){

              $a.="<font size=".$args["size"]."color=".$args["color"].">".$args['content']."</font><br>";

              }

              return$a;

       }

 

注册块:

       <hr/>

       <br/>

       <a></a>

      

a.html

<{h  num=10 content=”aaaa”}>

<{/h}>

 

 

<{*自定义块:hello*}>

<{hellonum="10" size="3"}>aaaaaaaaaaaaa<{/hello}>

 

       //注册块

       $tpl->register_block("hello","do_translation");

       //自定义函数

       functiondo_translation($args,$content,&$a,&$b) {

             $a="";

              for($i=0;$i<=$args["num"]; $i++){

                     $a.=$content;

              }

              return $a;

       }

 

 

 

将函数写成插件文件存放到plugins 目录下:

 

新建函数文件  world

新建函数文件 world

  例如:

function.assign_debug_info.php

function  smarty_function_***(){}

  步骤

第一步:plugins下新建文件function.world.php

第二步:打开文件

  

function smarty_function_world($args, &$smarty)

{

              $a="";

              for($i=0;$i<=$args["num"]; $i++){

                     $a.="<fontsize=".$args["size"]."color=".$args["color"].">".$args['content']."</font><br>";

              }

              return $a;

   

}

 

在模板里调用函数

<{title num="10" content="aaa" size="10"  color="green"}>

 

          新建块文件hello

block.hello.php

function  smarty_block_hello(){}

第一步:plugins下新建块文件block.hello.php

第二步:打开文件

 Function smarty_block_hello($args,$content,&$smarty){

$a="";

              for($i=0;$i<=$args["num"]; $i++){

                     $a.=$content;

              }

              return $a;

 

 }

 

在模板里调用块

<{hello num="10"size="3"}>aaaaaaaaaaaaa<{/hello}>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值