smarty,模板调用函数.doc
文本预览下载声明
smarty,模板调用函数
篇一:smarty模板的使用
Smarty模板使用细节
1. 解压后把libs文件夹放在项目文件夹内
2. 在同级目录下建立templates(放模板文件)和templates_c(放经过smarty编译后的php文件)文件夹
3. 在templates文件夹中建立模板文件以 .tpl 为扩展名
注意事项:
1. 替换变量的标识分隔符 默认是{}最好改为lt;{}
有两个方法:
一种是在smarty源代码中改动left_delimiter right_delimiter
另一种是动态修改:
$smarty=new Smarty;
$smarty-left_delimiter=lt;{;
$smarty-right_delimiter=};
$smarty-template_dir=templates;//模板文件所在目录
$smarty-compile_dir=templates_c;//编译后文件保存目录
Smarty 模板支持php所有的变量类型
Php基本数据:int double string bool
复合数据类型:array object
特殊数据类型:resource null
模板中取数组 对象
lt;br/*********************取索引数组*********************lt;br/
lt;plt;{$array1[0]}||lt;{$array1[1]}||lt;{$array1[2]}lt;/p
lt;hr/
lt;br/*********************取关联数组*********************lt;br/
lt;plt;{$array2.city1}||lt;{$array2.city2}||lt;{$array2.city3}lt;/p
lt;hr/
lt;br/*********************取二维索引数组*********************lt;br/
lt;plt;{$array3[0][0]}||lt;{$array3[1][1]}||lt;{$array[1][0]}lt;/p
lt;br/*********************取二维关联数组*********************lt;br/
lt;plt;{$array3[0].name}||lt;{$array3[1].name}||lt;{$array[1].city}lt;/p
lt;plt;{$[1]}||lt;{$array3[1].name}||lt;{$array[1].city}lt;/p
lt;br/*********************取对象*********************lt;br/
lt;plt;{$studnet-name}||lt;{$studnet-array1[1]}||lt;{$student-array1[1].name}
如何用模板直接从配置文件中取数据
当某个变量不希望直接固定到程序中,就可以通过配置文件来获取(通过 lt;{##}
1. 先写一个配置文件 my.conf
my.conf:
bgcolor=”grey”
获取)
hello.tpl:
首先载入my.conf: lt;{config_load file=../config/my.conf}
再显示:lt;style
body{
background:lt;{#background#};
}
lt;/style
如何取出保留的变量lt;php的全局变量
http://localhost/demo.php?page=1
demo.php
lt;?php
$smarty-display(demo.tpl);
?
demo.tpl
lt;{$smarty.get.page}
lt;{$}
lt;{$smarty.server.SERVER_NAME}
Smarty 模板内建函数的使用
in_build
lt;h1内建函数foreach的使用lt;/h1
lt;h3一维索引数组lt;/h3
lt;{foreach from=$array1 item=temp key=k}
lt;{$k}=lt;{$temp}
lt;{/foreach}
lt;h3一维关联数组lt;/h3
lt;{foreach from=$array2 item=temp key=k}
lt;{$k}=lt;{$temp}
lt;{/foreach}
lt;h3二维数组 amp; if el
显示全部