PHP头条
热点:

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


Smarty自带一些 内建函数.
内建函数是模板语言的一部分.
用户不能创建名称和 内建函数一样的自定义函数,也不能修改 内建函数 .
(insert 、if,elseif,else、ldelim,rdelim、literal、php 、section,sectionelse 、strip 等内置函数请参考php模板引擎smarty的内置函数之二 )
#capture函数
config_load
foreach,foreachelse
include
include_php
 
capture
Attribute Name Type Required Default Description
name string no default  The name of the captured block
assign string No n/a  The variable name where to assign the captured output to 属性 类型 是否必须 缺省值 描述
name string no default  数据采集区域名称
assign string No n/a  数据采集区域在哪分配给变量name[待考] capture函数的作用是捕获模板输出的数据并将其存储到一个变量里,而不是把它们输出到页面.
任何在 {capture name="foo"}和{/capture}之间的数据将被存储到变量$foo中,该变量由name属性指定.
在模板中通过 $smarty.capture.foo 访问该变量.
如果没有指定 name 属性,函数默认将使用 "default" 作为参数.
{capture}必须成对出现,即以{/capture}作为结尾,该函数不能嵌套使用.
例:
{* 该例在捕获到内容后输出一行包含数据的表格,如果没有捕获到就什么也不输出 *}
{capture name=banner}
{include file="get_banner.tpl"}
{/capture}
{if $smarty.capture.banner ne ""}


{$smarty.capture.banner}


{/if}
config_load
从配置文件中加载变量
属性 类型 是否必须 缺省值 描述
file string Yes n/a  待包含的配置文件的名称
section string No n/a  配置文件中待加载部分的名称
scope string no local  加载数据的作用域,取值必须为local, parent 或 global. local 说明该变量的作用域为当前模板. parent 说明该变量的作用域为当前模板和当前模板的父模板(调用当前模板的模板). global 说明该变量的作用域为所有模板. 
global boolean No No  说明加载的变量是否全局可见,等同于 scope=parent. 注意: 当指定了 scope 属性时,可以设置该属性,但模板忽略该属性值而以 scope 属性为准。
例:
{config_load file="colors.conf"}

{#pageTitle#}







First Last Address


配置文件有可能包含多个部分,此时可以使用附加属性 section 指定从哪一部分中取得变量.
注:配置文件中的 section 和模板内建函数 section 只是命名相同,毫不相干。
例:
{config_load file="colors.conf" section="Customer"}

{#pageTitle#}







First Last Address


foreach,foreachelse foreach 是除 section 之外处理循环的另一种方案(根据不同需要选择不同的方案).
foreach 用于处理简单数组(数组中的元素的类型一致),它的格式比 section 简单许多,缺点是只能处理简单数组.
foreach 必须和 /foreach 成对使用,且必须指定 from 和 item 属性.
name 属性可以任意指定(字母、数字和下划线的组合).
foreach 可以嵌套,但必须保证嵌套中的 foreach 名称唯一.
from 属性(通常是数组)决定循环的次数.
foreachelse 语句在 from 变量没有值的时候被执行.
属性 类型 是否必须 缺省值 描述
from string Yes n/a  待循环数组的名称
item string Yes n/a  当前处理元素的变量名称
key string No n/a  当前处理元素的键名
 
name string No n/a  该循环的名称,用于访问该循 例1:
{* 该例将输出数组 $custid 中的所有元素的值 *}
{foreach from=$custid item=curr_id}
id: {$curr_id}

{/foreach}
输出结果:
id: 1000

id: 1001

id: 1002

例2:
$smarty->assign("contacts", array(array("phone" => "1", "fax" => "2", "cell" => "3"),
array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234")));
*}
{* 键就是数组的下标,请参看关于数组的解释 *}
{foreach name=outer item=contact from=$contacts}
{foreach key=key item=item from=$contact}
{$key}: {$item}

{/foreach}
{/foreach}
输出结果:
phone: 1

fax: 2

cell: 3

phone: 555-4444

fax: 555-3333

cell: 760-1234
foreach 循环有自己的变量名,使用该变量名可以访问该循环. 使用方法为{$smarty.foreach.foreachname.varname},其中 foreachname 即在 foreach 中指定的 name 属性.
include
Include 标签用于在当前模板中包含其它模板. 当前模板中的变量在被包含的模板中可用. 必须指定 file 属性,该属性指明模板资源的位置.
如果设置了 assign 属性,该属性对应的变量名用于保存待包含模板的输出,这样待包含模板的输出就不会直接显示了。
属性 类型 是否必须 缺省值 描述
file string Yes n/a  待包含的模板文件名
assign string No n/a  该属性指定一个变量保存待包含模板的输出
[var ...] [var type] No n/a  传递给待包含模板的本地参数,只在待包含模板中有效 例1 include 函数演示
{include file="header.tpl"}
{* body of template goes here *}
{include file="footer.tpl"} 例2带传递参数的 include 函数演示
{include file="header.tpl" title="Main Menu" table_bgcolor="#c0c0c0"}
{* body of template goes here *}
{include file="footer.tpl" logo="http://www.php118.com/php118.gif"} 例3使用外部模板资源的 include 函数演示
{* absolute filepath *}
{include file="/usr/local/include/templates/header.tpl"}
{* absolute filepath (same thing) *}
{include file="file:/usr/local/include/templates/header.tpl"}
{* windows absolute filepath (MUST use "file:" prefix) *}
{include file="file:C:/www/pub/templates/header.tpl"}
{* include from template resource named "db" *}
{include file="db:header.tpl"} include_php
inluce_php 函数用于在模板中包含 php 脚本. 如果设置了安全模式,被包含的脚本必须位于 $trusted_dir 路径下. include_php 函数必须设置 file 属性,该属性指明被包含 php 文件的路径,可以是 $trusted_dir 的相对路径,也可以是绝对路径.
include_php 是解决模板部件化的好方法,它使得 php 代码从模板文件中被分离出来. 举个例子:假设有一个从数据库中动态取出数据用于显示站点导航的模板,你可以将得数据内容的 php 逻辑部分分离出来保存在一个单独的文件夹下,并在模板开始的位置包含该 php 脚本. 那么就可以在任何地方包含此模板而不用担心之前数据库信息是否已被程序取出.
即使是在模板中多次地调用 php 文件,默认情况下它们只被包含一次. 你可以设置 once 属性从而指明每次调用都重新包含该文件. 如果将 once 属性设置为 false,每次调用该文件都将被重新包含.
如果设置了 assign 属性,该属性对应的变量名用于保存待包含 php 的输出,这样待包含 php 文件的输出就不会直接显示了。
在待包含 php 文件中可以通过 $this 访问 smarty 对象.
属性 类型 是否必须 缺省值 描述
file string Yes n/a  待包含php文件的名称
once boolean No true  如果待包含php文件已被包含是否仍然包含(类似php中的include_once函数)
assign string No n/a  该属性指定一个变量保存待包含php文件的输出 例
// load in variables from a mysql db and assign them to the template
// 从mysql数据库中取得数据,将数据赋给模板变量 require_once("MySQL.class.php");
$sql = new MySQL;
$sql->query("select * from site_nav_sections order by name",SQL_ALL);
$this->assign('sections',$sql->record);
?> index.tpl
---------
{* absolute path, or relative to $trusted_dir *}
{* 绝对路径或 $trusted_dir 的相对路径 *}
{include_php file="/path/to/load_nav.php"}
{foreach item="curr_section" from=$sections}
{$curr_section.name}

{/foreach}

www.phpzy.comtrue/phpkj/10925.htmlTechArticlephp模板引擎smarty的内置函数之一 Smarty自带一些 内建函数. 内建函数是模板语言的一部分. 用户不能创建名称和 内建函数一样的自定义函数,也不能修改 内建函数 . (insert 、if,elseif,else、...

相关文章

相关频道:

PHP之友评论

今天推荐