一,条件判断
条件修饰符有:eq(==), neq(!=) gt(>), lt(<)
php: $smarty->assign('score', 60); tpl: {if $score gt 70} 优秀 {elseif $score gt 60} 及格 {else} 不及格 {/if}
show:
条件修饰符必须用空格与变量和常量分开。
二,循环语句
1,section循环
php: $articlelist = array( array( "title" => "第一篇", "author" => "小王", "content" => "这是第一篇文章" ), array( "title" => "第二篇", "author" => "小马", "content" => "这是第二篇文章" ) ); $smarty->assign('articlelist', $articlelist); tpl: {section name=article loop=$articlelist} {$articlelist[article].title} {$articlelist[article].author} {$articlelist[article].content} <br> {/section}
show:
没有数据时,可以用{sectionelse}
section中的属性:
name:(必选) 是section循环的名称只是标示循环唯一的名字没有特别意义,前面没有$符号;
loop: (必选)是在php声明中的变量名称,用来标示是循环哪一个数组(即要循环数组名)需要使用$;
start: (可选)循环执行的初始位置. 如果该值为负数,开始位置从数组的尾部算起. 例如:如果数组中有7个元素,指定start为-2,那么指向当前数组的索引为5. 非法值(超过了循环数组的下限)将被自动调整为最接近的合法值.
step: (可选)如其它语言的循环,是一个步长,如果为负数,则倒序循环;
max:(可选)循环的最大下标,如果是1则只循环1次,如果为2则循环2次;
show:(可选)默认为true即显示。如果设置了{sectionelse}。表示如果数组没有内容的时候显示这部分的内容;如果show为false则显示这部分。如果没有设置{sectionelse}则不输出该数组。
2,foreach循环(可以使用PHP语法)
(1)
tpl:
{foreach item=article from=$articlelist}
{$article.title}
{$article.author}
{$article.content}
<br>
{foreachelse}
当前没有文章
{/foreach}
(2)php原生
tpl: {foreach $articlelist as $article} {$article.title} {$article.author} {$article.content} <br> {/foreach}
tpl:
{foreach $names as $top=>$name} {$top} {$name} <br> {/foreach}
三,文件引用:在Smarty语法中,引入函数只有include。
tpl1:
{include file="header.tpl" sitename="慕课网"}
tpl2:
{$sitename}
show: