smarty学习2
2011-08-16 17:09 呦菜 阅读(497) 评论(0) 编辑 收藏 举报smarty模板:
1. tpl: <{* 这是注释格式 *}>
1. tpl: <{* 这是注释格式 *}>
2. tpl:
<{ include
file="bottom.htm"}>
将bottom.htm模板文件包含到当前模板文件中
3.
php:
$smarty->assign("name","李晓军");将模板中的$name替换成"李晓军"三个字
$smarty->display("tpl.htm");
编译并显示模板文件tpl.htm
4. tpl:
<{$title|capitalize}>
首字母大写
<{$title|lower}>
全部变小写
<{$title|upper}>
全部变大写
<{$title|cat:"made by
chenda"}>
将cat里的值连接到给定的变量后面
<{$smarty.now|date_format:"%Y-%m-%d"}>
显示当前日期
<{$content|default:"没有指定文章内容"}>
当变量为空或者未分配时,由给定的默认值替代输出
<{$content|indent:6}>
或
<{$content|indent:2:" "}>
<{$title|replace:"teacherli":"李晓军"}>
把变量中的teacherli替换成:李晓军
5. <{assign var="myname"
value="陈达"}>
在模板内定义一个变量
输出该变量:<{$myname}>
6. 显示复选框
tpl:
<{html_checkboxes labels=false
name="aa" options=$cust_checkboxes
checked=$select_id
separator="<br>"}>
php:
$smarty->assign('cust_checkboxes',
array(1000 => 'Joe Schmoe',1001
=> 'Jack Smith'));
定义关联数组
$smarty->assign('select_id',
1001); 指定默认值
7. 显示单选框
tpl:
<{html_radios name="aa" labels=false
options=$cust_radios
checked=$customer_id
separator="<br>"}>
php:
$smarty->assign('cust_radios',
array(1001 => 'Joe Schmoe',1002
=> 'Jack Smith',1003 => 'Jane
Johnson'));
$smarty->assign('customer_id',
1001);
$smarty->a
8.针对一维数组的循环:
foreach循环:鸟巢式的循环,一层一层往下走
$array1 = array(1 => "苹果", 2 => "菠萝", 3 => "香蕉", 4 => "芭乐");
$tpl->assign("array1", $array1);
如:{foreach from=要循环的数组变量,
item=要循环的变量名称}
{ $要循环的变量名称
}
{/foreach}
$tpl->assi
<{foreach item=item1 from=$array1}>
<{$item1}>
<{/foreach}>
section循环:<{section name=
“section的名称,不用加$”
loop=
“要循环的变量,在程序中要使用assign对这个变量进行操作”}>
<{$item1}
<{/foreach
section循环:
{$loopname[name].var}//loopname:loop处赋的名字;name:name处赋的名字;var:下标名
{/section}
<{section name=sec1 loop=$array1}>
<{$array1[sec1]}>
<{/section}>
针对二维数组的循环:
如:
$array2= array(
array("index1" => "data1-1", "index2" => "data1-2", "index3" => "data1-3"),
array("index1" => "data2-1", "index2" => "data2-2", "index3" => "data2-3"),
array("index1" => "data3-1", "index2" => "data3-2", "index3" => "data3-3"),
array("index1" => "data4-1", "index2" => "data4-2", "index3" => "data4-3"),
array("index1" => "data5-1", "index2" => "data5-2", "index3" => "data5-3"));
$tpl->assign("array2", $array2);
foreach 来呈现 array2 :
<{foreach item=index2 from=$array2}>
<{foreach key=key2 item=item2 from=$index2}>
<{$key2}>: <{$item2}>
<{/foreach}>
<{/foreach}>
section
来呈现 array2 :
<{section name=sec2 loop=$array2}>
index1:<{$array2[sec2].index1}>
index2:<{$array2[sec2].index2}>
index3:<{$array2[sec2].index3}>
<{/section}>
section则以
[主数组][循环名称].子数组索引
将整个数组呈现出来
注: section 的数组索引必须是从 0 开始的正整数,即 0, 1, 2, 3, ...如果您的数组索引不是从 0 开始的正整数,那么就得改用 foreach 来呈现.
<{$array
<{/sectio
$array2
array("i
array("i
array("i
array("i
array("i
$tpl->as
<{foreac
<{foreac
<{$key2}
<{/forea
<{/forea
section
<{sectio
index1:
index2:
index3:
<{/secti
section
转自:http://blog.sina.com.cn/s/blog_436732df0100ea0n.html