导读:smarty 初始化配置smarty 模板设计的基本语法1, 模板中的 注释 {* smarty 模板注释 *}2, 模板中的 变量应用 php: html: {$tit...
smarty 初始化配置
smarty 模板设计的基本语法
1, 模板中的 注释 {* smarty 模板注释 *}
2, 模板中的 变量应用
php: html: {$title}
require "init.inc.php";
$smarty->assign("title","这是变量的值");
$smarty->display("index.php");
3, 模板中的 函数应用
php: function myfun(){
return date("");
}
smarty 模板中的变量应用
1, 从配置文件中 .ini .conf #注释 titlecolor=#000000 [b]tablebgcolor=#000000
2, 在模板中使用保留变量
php:
$array=array("aaa","bbb","ccc");
$smarty->assign("title","$array");
$smarty->display("index.php");
html:
{$title[0]}<br/>
{$title[1]}<br/>
{$smarty->name} 属性 {$smarty->say()}
表达式{$x+$y} {$foo=strlen($bar)} 双引号嵌入表达式{func var="test $foo test"}
双引号中使用标签 {$foo={counter}+3} 可变变量 $foo_{$bar}
php:
require"init.inc.php";
$smarty->assign("title","内容")
$smarty->display("index.html")
html:
{$title}<br/>
配置文件:foo.ini
#..... 注释内容
............ 以下全局变量
............
[节]
tablebgcolor=#00000
....................
....................
html:
{config_load file="foo.ini"}或者{config_load "foo.ini"}
{config_load file="foo.ini" section="节"}
{#tablebgcolor#}或者{$smarty.config.tablebgcolor}
保留变量:
{$smarty.get.page} {$smarty.session.username}等等。
smarty 模板中的函数应用
php:
function myfunc(){
return date("Y-m-d");
}
$smarty->display("index.html");
html:
{date("Y-m-d H:i:s")}
{myfunc()}
1,函数 ---- 相当于HTML 独立标签
{config_load file="init.ini"}
{include file="top.html"}
{include file="foot.html"}
2,块函数 ---- 相当于HTML 闭合标签
{nocache}
{$smarty.now|date_format}
{/nocache}
{if $hight_name}
welcome,<font color="{#fontcolor#}">{$name}</font>
{else}
welcome{$name}
{/if}
smarty 模板中的变量调解器 "也称为变量修改器"
1,变量调解器函数使用方法
{变量|修改器:"参数1":"参数2":...}
{变量|修改器1|修改器2|修改器3}
{$var|modifier:"args1":"args2":...} 修改器及参数
{$var|modifier1|modifier2|modifier3|...} 多个调解器或者多个修改器
{$var|函数:arg2:arg3:....}
2,默认的变量调解器
部分非常常用: date_formate truncate escaps regex_replace
3,自写义变量调解器插件
registe plugin()方法扩展 定义为修改器/变量调解器函数
自定义变量调解器函数 注册为smarty变量调解器或变量修改器
<?php
require "init.inc.php";
$smarty->assign("var","哦数据");
$smarty->registerplugin("modifier","mystyle","test");
function test($var,$color,$size){
return '<font color="'.$color.'" size="'.$size.'" >'.$var.'</font>';
}
$smarty->display("index.html");
?>
{$var}
{$var|myfun}
{$var|myfun:'red'}
{$var|myfun:'blue':'7'}
<{$var|substr:0:10}> 等同 <{substr($var,010)}>
组合变量修改器
{$var|myfun:'green'|substr:0:20|preplace:'/\d/':'@'}
smarty 自定义函数
1,为smarty模板扩充函数插件
2, 为smarty模板扩充块函数插件
smarty 模板中的内置函数
1,变量声明
2,流程控制
3,声明和调用模板函数
4,数组遍历
5,其它内置函数
smarty 缓存控制
1, smarty中控制缓存
2, 每个模板多个缓存
3, 为缓存实例消除处理开销
4, 清除缓存
5, 关闭局部缓存