快捷輸出變量
{:function(…)} //執行方法並輸出返回值
{~function} //執行方法不輸出
{@var} //輸出Session變量
{#var} //輸出Cookie變量
{&var} //輸出配置參數
{%var} //輸出語言變量
{.var} //輸出GET變量
{^var} //輸出POST變量
{*var} //輸出常量
模版輸出
//1 直接調用index操作模板
$this->display();
// 對應Myapp/Tpl/default/Index/index.html
//2 調用Index模塊的test1操作模板
$this->display('test1');
//對應Myapp/Tpl/default/Index/test1.html
//2 調用Message模塊的test2操作模板()
$this->display('Message:test2');
//對應Myapp/Tpl/default/Message/test2.html
//3 調用XP主題的Message模塊的test2操作模板
$this->display('Xp@Message:test2');
//對應Myapp/Tpl/Xp/Message/test2.html
//4 直接指定模板文件的全名
$this->display('../Message/test3.html');
//對應Myapp/Tpl/default/Message/test3.html
使用判斷語句
<if condition="($name eq 1) OR ($name gt 100) "> value1
<elseif condition="$name eq 2" />value2
<else /> value3
</if>
<if condition="$id < 5 "> value1
<else /> value2
</if>
必須改成:
<if condition="$id lt 5 "> value1
<else /> value2
</if>
<if condition="strtoupper($user['name']) neq 'THINKPHP' "> ThinkPHP
<else /> other Framework
</if>
<if condition="$user.name neq 'ThinkPHP' "> ThinkPHP
<else /> other Framework
</if>
<if condition="$user:name neq 'ThinkPHP' "> ThinkPHP
<else /> other Framework
</if>
由於if標籤的condition屬性裏面基本上使用的是php語法,儘可能使用判斷標籤和Switch標籤會更加簡潔,原則上來說,能夠用
switch和比較標籤解決的儘量不用if標籤完成。因爲switch和比較標籤可以使用變量調節器和系統變量。如果某些特殊的要求下面,IF標籤仍然無
法滿足要求的話,可以使用原生php代碼或者PHP標籤來直接書寫代碼。
eq 等於(==)
neq 不等於(!=)
gt 大於(>)
egt 大於等於(>=)
lt 小於(<)
elt 小於等於(<=)
heq 恆等於(===)
nheq 不恆等於(!==)
condition 條件
注意
condition 屬性值中,變量是需要 $ 符號的,這與其他標籤不同。
使用自定義函數的方法
模板變量的函數調用格式:{$varname|function1|function2=arg1,arg2,### }
使用例子:
{$webTitle|md5|strtoupper|substr=0,3}
{$number|number_format=2}
{$varname|function1|function2=arg1,arg2,### }
實例:
function Cate($cid){
$Cate=D('Cate');
$Cate=$Cate->where('id='.$cid)->find();
return $Cate['title'];
}
我想在模板中調用這個函數則在模板中可以這樣寫
{$vo.cid|cate=###}
注意:自定義函數要放在項目應用目錄/common/common.php中。 這裏是關鍵。
說明:
{ 和 $ 符號之間不能有空格,後面參數的空格就沒有問題;
###表示模板變量本身的參數位置 ;
支持多個函數,函數之間支持空格 ;
支持函數屏蔽功能,在配置文件中可以配置禁止使用的函數列表 ;
支持變量緩存功能,重複變量字串不多次解析。