Heim  >  Artikel  >  Backend-Entwicklung  >  php—Smarty-4 (21)

php—Smarty-4 (21)

WBOY
WBOYOriginal
2016-08-08 09:24:561085Durchsuche

Smarty函数---插件

1)  内建函数                   sysplugins              不允许修改

2)  自定义函数                   plugins                  可以修改

1、capture             捕获一段内容,但不输出

语法:

{capture name=‘var’}

              内容

{/capture}

Name:将内容保存到指定的变量中

{$smarty.capture.var}

 

在模板中,需要反复输出的大量内容,可以考虑使用capture捕获。

2、config_load             加载配置文件

{config_load file=“file”section=“section”}

file:配置文件名称

section:引用哪个配置节 可选参数

引用语法:

{#var#}

{$smarty.config.var}

3、foreach,foreachelse          用于遍历数组

语法:

{foreach from=数组 key=键 name=名称 item=内容 } 

              {foreachelse} 当要遍历的数组没有任何元素时,执行foreachelse后面的语句

{/foreach}

From:要遍历的数组

Item:遍历到的键值

Key:遍历到的键名

Name:为foreach语句命名

Foreach内置变量

$smarty.foreach.name.index :索引  从0开始

$smarty.foreach.name.iteration  :索引  从1开始

$smarty.foreach.name.first :当第一次执行循环时,该值为true

$smarty.foreach.name.last :当最后一次执行循环时,该值为true

$smarty.foreach.name.total :循环总数

 

4、include  引用另一个模板文件

{include file=“file” assign=“var” [var ...]}

File:要引入的模板文件

Assign:将结果保存,但本次不输出

[var …] :可选参数,为模板文件传参

5、include_php         包含一个php文件

{ include_php file=“file” assign=“var”      once=“true|false”}

File:要引入的php文件

Assign:保存输出结果,但本次不输出

Once:是否只包含一次   true:是     false:否

这个函数在smarty2.6中是可用的,但在3.0中将这个函数移除,如果还想使用这个函数,可以引用SmartyBC.class.php这个类文件

6、insert     在模板中,执行一个php函数

语法:

{ insert name=“func”   [var ...] }

Name :被调用的php函数名

[var ...] :调用函数时指定参数  可选参数

Insert和include_php是同样的。都是2.6中的函数,在3.0中,将这个功能放到SmartyBC.class.php这个文件中了。

注意:inser引用的php函数的名称必须以insert_为前缀

7、if elseif else

       {if}

       {elseif}

       {else}

       {/if}

8、ldelim 、rdelim   模板中的 { }  的转义符

{ldelim}

{rdelim} 

9、literal   literal标记中间的内容不会被smarty所解析

       {literal}

              ……

       {/literal}

10、php    可以在一对php标记中写php代码

      

       {php}

              ……

      {/php}

     

以上就介绍了php—Smarty-4 (21),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn