Rumah  >  Artikel  >  pembangunan bahagian belakang  >  php模板引擎smarty的内置函数之二

php模板引擎smarty的内置函数之二

黄舟
黄舟asal
2016-12-20 10:56:551337semak imbas

Smarty自带一些 内建函数. 
内建函数是模板语言的一部分. 
用户不能创建名称和 内建函数一样的自定义函数,也不能修改 内建函数 .
(#capture函数、config_load 、foreach,foreachelse、include、include_php等内置函数请参考php模板引擎smarty使用教程专题之 内置函数之一)
insert 
if,elseif,else
ldelim,rdelim
literal
php 
section,sectionelse 
strip 
 
insert
属性 类型 是否必须 缺省值 描述 
name string Yes n/a  插入函数的名称 
assign string No n/a  该属性指定一个变量保存待插入函数输出 
script string No n/a  插入函数前需要先包含的php脚本名称
 
[var ...] [var type] No n/a  传递给待插入函数的本地参数

Insert 函数类似欲 inluce 函数,不同之处是 insert 所包含的内容不会被缓存,每次调用该模板都会重新执行该函数.

例如你在页面上端使用一个带有广告条位置的模板,广告条可以包含任何HTML、图象、FLASH等混合信息. 因此这里不能使用一个静态的链接,同时我们也不希望该广告条被缓存. 这就需要在 insert 函数指定:#banner_location_id# 和 #site_id# 值(从配置文件中取),同时需要一个函数取广告条的内容信息.
 
insert 函数演示 
{* example of fetching a banner *} {insert name="getBanner" lid=#banner_location_id# sid=#site_id#}

在此例中,我们使用了 getBanner 作为 name 属性,同时传递了 #banner_location_id# 和 #site_id# 两个参数. 接下来 Smarty 在你的 php 程序中搜索名为 insert_getBanner() 的函数,#banner_location_id# 和 #site_id# 的值被组合成一个数组作为函数的第一个参数传递给该函数. 为了避免函数命名混乱,所有的 insert 函数都必须以 insert_ 开头. 你的 insert_getBanner() 函数根据传递的参数执行并返回执行的结果. 这些结果就显示在模板中调用该函数的位置. 在此例中 Smarty 调用该函数类似insert_getBanner(array("lid"=>"12345","sid"=>67890"));并将返回的结果显示在调用的位置. 
 
如果设置了 assign 属性,该属性对应的变量名用于保存待包含函数的输出,这样待包含函数的输出就不会直接显示了.注意:赋给模板变量的输出信息在缓存的时候同样无效.
 
如果指定了 script 属性,在调用函数并执行前将先包含(只包含一次)script指定的 php 脚本. 这是为了防止被调用的函数不存在,先调用包含该函数的 php 脚本将避免该情况.
 
Smart 对象作为函数的第二个参数被传递,在待包含函数中可以通过 $this 访问并修改 smarty 对象信息.
技术要点: 使模板的一部分不被缓存. 如果打开了缓存, insert 函数却不会被缓存,每次调用页面它们都会被动态加载,即使是在缓存页面中. 该特性可以广泛应用于广告条、投票、实时天气预报、搜索结果、反馈信息等区域.
 
if,elseif,else
 
Smarty 中的 if 语句和 php 中的 if 语句一样灵活易用,并增加了几个特性以适宜模板引擎. if 必须于 /if 成对出现. 可以使用 else 和 elseif 子句. 可以使用以下条件修饰词:eq、ne、neq、gt、lt、lte、le、gte、ge、is even、is odd、is not even、is not odd、not、mod、div by、even by、odd by、==、!=、>、<、<=、>=. 使用这些修饰词时必须和变量或常量用空格格开.
语句演示 {if $name eq "Fred"} Welcome Sir.{elseif $name eq "Wilma"} Welcome Ma'am.{else} Welcome, whatever you are.{/if}{* an example with "or" logic *}{if $name eq "Fred" or $name eq "Wilma"} ...{/if}{* same as above *}{if $name == "Fred" || $name == "Wilma"} ...{/if}{* the following syntax will NOT work, conditional qualifiers must be separated from surrounding elements by spaces *}{if $name=="Fred" || $name=="Wilma"} ...{/if}{* parenthesis are allowed *}{if ( $amount < 0 or $amount > 1000 ) and $volume >= #minVolAmt#} ...{/if}{* you can also embed php function calls *}{if count($var) gt 0} ...{/if}{* test if values are even or odd *}{if $var is even} ...{/if}{if $var is odd} ...{/if}{if $var is not odd} ...{/if}{* test if var is divisible by 4 *}{if $var is div by 4} ...{/if}{* test if var is even, grouped by two. i.e.,0=even, 1=even, 2=odd, 3=odd, 4=even, 5=even, etc. *}{if $var is even by 2} ...{/if}{* 0=even, 1=even, 2=even, 3=odd, 4=odd, 5=odd, etc. *}{if $var is even by 3} ...{/if}

ldelim,rdelim
ldelim 和 rdelim 用于输出分隔符,也就是大括号 "{" 和 "}". 模板引擎总是尝试解释大括号内的内容,因此如果需要输出大括号,请使用此方法.
使用 ldelim, rdelim 演示 {* this will print literal delimiters out of the template *}{ldelim}funcname{rdelim} is how functions look in Smarty!输出结果:{funcname} is how functions look in Smarty!


literal
Literal 标签区域内的数据将被当作文本处理,此时模板将忽略其内部的所有字符信息. 该特性用于显示有可能包含大括号等字符信息的 javascript 脚本. 当这些信息处于 {literal}{/literal} 标签中时,模板引擎将不分析它们,而直接显示.
 
literal 标签演示 
{literal}
 
{/literal}
 

php

php 标签允许在模板中直接嵌入 php 脚本. 是否处理这些语句取决于$php_handling的设置. 该语句通常不需要使用,当然如果你非常了解此特性或认为必须要用,也可以使用.

php 标签演示 
{php} // including a php script directly // from the template. include("/path/to/display_weather.php"); {/php}


section,sectionelse
模板的 section 用于遍历数组中的数据. section 标签必须成对出现. 必须设置 name 和 loop 属性. 名称可以是包含字母、数字和下划线的任意组合. 可以嵌套但必须保证嵌套的 name 唯一. 变量 loop (通常是数组)决定循环执行的次数. 当需要在 section 循环内输出变量时,必须在变量后加上中括号包含着的 name 变量. sectionelse 当 loop 变量无值时被执行.

属性 类型 是否必须 缺省值 描述 
name string Yes n/a  该循环的名称 
loop [$variable_name] Yes n/a  决定循环次数的变量名称 
start integer No 0  循环执行的初始位置. 如果该值为负数,开始位置从数组的尾部算起. 例如:如果数组中有7个元素,指定start为-2,那么指向当前数组的索引为5. 非法值(超过了循环数组的下限)将被自动调整为最接近的合法值. 
step integer No 1  该值决定循环的步长. 例如指定step=2将只遍历下标为0、2、4等的元素. 如果step为负值,那么遍历数组的时候从后向前遍历. 
max integer No 1  设定循环最大执行次数. 
show boolean No true  决定是否显示该循环.


section 函数演示
{* this example will print out all the values of the $custid array *}
{section name=customer loop=$custid}
 id: {$custid[customer]}

{/section}

输出结果:

id: 1000

id: 1001

id: 1002

loop演示
输出结果:
{* the loop variable only determines the number of times to loop. you can access any variable from the template within the section. This example assumes that $custid, $name and $address are all arrays containing the same number of values *}{section name=customer loop=$custid} id: {$custid[customer]}
name: {$name[customer]}
address: {$address[customer]}

{/section}输出结果:id: 1000
name: John Smith
address: 253 N 45th

id: 1001
name: Jack Jones
address: 417 Mulberry ln

id: 1002
name: Jane Munson
address: 5605 apple st

section 名称演示 {* the name of the section can be anything you like,
 and it is used to reference the data within the section *}
{section name=mydata loop=$custid}
 id: {$custid[mydata]}

 name: {$name[mydata]}

 address: {$address[mydata]}

 


{/section} 
嵌套 section 演示 {* sections can be nested as deep as you like. With nested sections, you can access complex data structures, such as multi-dimensional arrays. In this example, $contact_type[customer] is an array of contact types for the current customer. *}{section name=customer loop=$custid} id: {$custid[customer]}
name: {$name[customer]}
address: {$address[customer]}
{section name=contact loop=$contact_type[customer]}  {$contact_type[customer][contact]}: {$contact_info[customer][contact]}
{/section}

{/section}输出结果:id: 1000
name: John Smith
address: 253 N 45th
home phone: 555-555-5555
cell phone: 555-555-5555
e-mail: john@mydomain.com

id: 1001
name: Jack Jones
address: 417 Mulberry ln
home phone: 555-555-5555
cell phone: 555-555-5555
e-mail: jack@mydomain.com

id: 1002
name: Jane Munson
address: 5605 apple st
home phone: 555-555-5555
cell phone: 555-555-5555
e-mail: jane@mydomain.com

 
section 遍历多维数组演示 {* This is an example of printing an associative array
 of data within a section *}
{section name=customer loop=$contacts}
 name: {$contacts[customer].name}

 home: {$contacts[customer].home}

 cell: {$contacts[customer].cell}

 e-mail: {$contacts[customer].email}


{/section}


输出结果:

name: John Smith

home: 555-555-5555

cell: 555-555-5555

e-mail: john@mydomain.com


name: Jack Jones

home phone: 555-555-5555

cell phone: 555-555-5555

e-mail: jack@mydomain.com


name: Jane Munson

home phone: 555-555-5555

cell phone: 555-555-5555

e-mail: jane@mydomain.com

 
sectionelse 演示 {* sectionelse will execute if there are no $custid values *}
{section name=customer loop=$custid}
 id: {$custid[customer]}

{sectionelse}
 there are no values in $custid.
{/section}

Section 循环也有可供调用的变量名. 通过如下方式调用{$smarty.section.sectionname.varname}.
 
strip
Web 开发者多次遇到空格和回车影响HTML输出的情形(浏览器的"特性"),为了得到特定的结果,因此你不得不在模板里运行所有的标签. 通常在难以理解或难以处理的模板中遇到此问题.

Smarty 在显示前将除区任何位于 {strip}{/strip} 标记中数据的首尾空格和回车. 这样可以保证模板容易理解且不用担心多余的空格导致问题.
strip 标签演示 {* the following will be all run into one line upon output *}
{strip}


 
  
 

       This is a test
   

  

{/strip}


输出结果:

{strip}

 
  
 

       This is a test
   

  

{/strip}


输出结果:

请注意上例,所有行都以HTML标签开头结尾. 所有行被组织到一起运行. 如果在行首和行尾有文本的话,它们也会被组织到一起,就有可能得到你不想得到的结果.

 以上就是php模板引擎smarty的内置函数之二的内容,更多相关内容请关注PHP中文网(www.php.cn)! 


Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn