smarty中的 判断 循环 与常见问题

来源:互联网 发布:日语翻译网络兼职 编辑:程序博客网 时间:2024/05/17 04:12

判断  

模板文件中可以使用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相等,
  ne、neq不相等,
  gt大于,
  lt小于,
  gte、ge大于等于,
  lte、le 小于等于,
  not非, mod求模。
  is [not] div by是否能被某数整除,
  ,B z M E m I w0 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.
  {else}
  Welcome,whatever you are.
  {/if}

循环

  在Smarty里使用循环遍历数组的方法是section,如何赋值遍历都是在模板中解决,php源文件中只要一个assign就能解决问题。
  示例:
  {* this examplewill print out all the values of the $custid array *}
  {section name=customer loop=$custid}
  id: {$custid[customer]}<br>
  {/section}
  OUTPUT:
  id: 1000<br>id: 1001<br>id: 1002<br>

常见问题

  Smarty将所有大括号{}里的东西都视为自己的逻辑程序,于是我们在网页中想插入javascript函数就需要literal的帮忙了,literal的功能就是忽略大括号{}。
  示例:
  {literal}
  <script language=javascript>
  function isblank(field) {
  if (field.value == '') { return false;
  }else{
  document.loginform.submit();
  return true;
  }
  }
  </script>
  {/literal}