首页
技术库 |  站长工具 | 技术手册 |  字体库 |  知识点词汇表  |  联系我们 |   



主菜单


站点首页
技术手册
字体库
知识点 词汇表
站长工具
高级搜索
联系我们
站点地图

文章分类



文章内容 

当前位置: .: PHP技术 .: 基础教程 .: php模板引擎smarty的内置函数之二
php模板引擎smarty的内置函数之二



php模板引擎smarty使用教程专题之 内置函数之二

Smarty自带一些 内建函数.
内建函数是模板语言的一部分.
用户不能创建名称和 内建函数一样的自定义函数,也不能修改 内建函数 .
(#capture函数、config_load 、foreach,foreachelse、include、include_php等内置函数请参考php模板引擎smarty使用教程专题之 内置函数之一)
 
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}
 <script language=javascript>
  <!--
  function isblank(field) {
  if (field.value == '')
  { return false; }
  else
  {
  document.loginform.submit();
  return true;
  }
  }
  // -->
 </script>
{/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 标签必须成对出现. 必须设置 nameloop 属性. 名称可以是包含字母、数字和下划线的任意组合. 可以嵌套但必须保证嵌套的 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]}<br>
{/section}

输出结果:

id: 1000<br>
id: 1001<br>
id: 1002<br>

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]}<br>
 name: {$name[customer]}<br>
 address: {$address[customer]}<br>
 <p>
{/section}

输出结果:
id: 1000<br>
name: John Smith<br>
address: 253 N 45th<br>
<p>
id: 1001<br>
name: Jack Jones<br>
address: 417 Mulberry ln<br>
<p>
id: 1002<br>
name: Jane Munson<br>
address: 5605 apple st<br>
<p>

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]}<br>
	name: {$name[mydata]}<br>
	address: {$address[mydata]}<br>
	<p>
{/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]}<br>
 name: {$name[customer]}<br>
 address: {$address[customer]}<br>
 {section name=contact loop=$contact_type[customer]}
  {$contact_type[customer][contact]}: {$contact_info[customer][contact]}<br>
 {/section}
 <p>
{/section}

输出结果:
id: 1000<br>
name: John Smith<br>
address: 253 N 45th<br>
home phone: 555-555-5555<br>
cell phone: 555-555-5555<br>
e-mail: john@mydomain.com<br>
<p>
id: 1001<br>
name: Jack Jones<br>
address: 417 Mulberry ln<br>
home phone: 555-555-5555<br>
cell phone: 555-555-5555<br>
e-mail: jack@mydomain.com<br>
<p>
id: 1002<br>
name: Jane Munson<br>
address: 5605 apple st<br>
home phone: 555-555-5555<br>
cell phone: 555-555-5555<br>
e-mail: jane@mydomain.com<br>
<p>
section 遍历多维数组演示
{* This is an example of printing an associative array
 of data within a section *}
{section name=customer loop=$contacts}
	name: {$contacts[customer].name}<br>
	home: {$contacts[customer].home}<br>
	cell: {$contacts[customer].cell}<br>
	e-mail: {$contacts[customer].email}<p>
{/section}


输出结果:

name: John Smith<br>
home: 555-555-5555<br>
cell: 555-555-5555<br>
e-mail: john@mydomain.com<p>
name: Jack Jones<br>
home phone: 555-555-5555<br>
cell phone: 555-555-5555<br>
e-mail: jack@mydomain.com<p>
name: Jane Munson<br>
home phone: 555-555-5555<br>
cell phone: 555-555-5555<br>
e-mail: jane@mydomain.com<p>
sectionelse 演示
{* sectionelse will execute if there are no $custid values *}
{section name=customer loop=$custid}
	id: {$custid[customer]}<br>
{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}
<table border=0>
	<tr>
		<td>
			<A HREF="{$url}">
			<font color="red">This is a test</font>
			</A>
		</td>
	</tr>
</table>
{/strip}


输出结果:

<table border=0><tr><td><A HREF="http://my.domain.com"><font color="redstrip 标签演示 
{* the following will be all run into one line upon output *}
{strip}
<table border=0>
	<tr>
		<td>
			<A HREF="{$url}">
			<font color="red">This is a test</font>
			</A>
		</td>
	</tr>
</table>
{/strip}


输出结果:

<table border=0><tr><td><A HREF="http://my.domain.com"><font color="red">This is a test</font></A></td></tr></table>

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





隐藏文章属性
文章编号:519
点击次数:707
创建日期:1-3-2008
修改日期:1-3-2008
发布人:laohan
点评:
发送此文
发表评论
打印
添加到收藏夹


评级:




用户评论

此文章还没有任何评论!

网站地图 - 知识词汇 - 全文检索 - 广告服务 - 帮助中心 - 联系我们
.:www.cn-web.com
网站技术开发联盟之WEB开发技术知识库
联系人:老韩(QQ:5679551)
晋ICP备07003487号