联系我们

当前位置: 首页 > ECshop经验共享 > Ecshop模板之Smarty运用(二)

Ecshop模板之Smarty运用(二)

二. 赋值

  在模板文件中需要替换的值用大括号{}括起来,值的前面还要加$号。例如{$hello}。这里可以是数组,比如{$hello.item1},{$hello.item2}…

  而PHP源文件中只需要一个简单的函数assign(var , value)。
简单的例子:
*.tpl:
Hello,{$exp.name}!Good {$exp.time}
*.php:
$hello[name]= “Mr. Green”;
$hello[time]=”morning”;
$smarty->assign(“exp”,$hello);
output:
Hello,Mr.Green!Good morning

三. 引用


网站中的网页一般header和footer是可以共用的,所以只要在每个tpl中引用它们就可以了。
示例:*.tpl:
{include file="header.tpl"}
{* body of template goes here *}
{include file="footer.tpl"}

 

四. 判断

 模板文件中可以使用if else等判断语句,即可以将一些逻辑程序放在模板里。"eq","ne", "neq", "gt", "lt","lte", "le", "gte" "ge","is even", "is odd", "is not even", "is notodd", "not", "mod", "div by", "evenby", "odd by","==","!=",">","<","<=",">="这些是if中可以用到的比较。看看就能知道什么意思吧。

eq相等,

neq不相等,


gt大于,


lt小于,


gte、ge大于等于,


lte、le 小于等于,


not非, mod求模。


is [not] div by是否能被某数整除,


is [not] even是否为偶数,


$a is [not] even by $b即($a / $b) % 2 == 0,


is [not] odd是否为奇,


$a is not odd by $b即($a / $b) % 2 != 0


示例:


  {if $name eq"Fred"}


  WelcomeSir.


  {elseif $name eq"Wilma"}


  WelcomeMa'am.


  Welcome,whatever you are.


  {/if}