PHP开发笔记's Archiver

fangzhen 发表于 2009-2-9 09:18

Thinkphp模板变量的高级使用

内置模板引擎对模板变量的支持相当充分,除了可以输出正常变量外,还可以输出特殊变量和对输出变量使用<STRONG><FONT color=#ff0000>函数</FONT></STRONG>定义(并支持多个<STRONG><FONT color=#ff0000>函数</FONT></STRONG>)。<BR>模板变量的标签格式为<BR>格式:{$varname|function1|function2=arg1,arg2,### } <BR>说明: <BR>{ 和 $ 符号之间不能有空格 ,后面<STRONG><FONT color=#ff0000>参数</FONT></STRONG>的空格就没有问题<BR>###表示模板变量本身的<STRONG><FONT color=#ff0000>参数</FONT></STRONG>位置 <BR>支持多个<STRONG><FONT color=#ff0000>函数</FONT></STRONG>,<STRONG><FONT color=#ff0000>函数</FONT></STRONG>之间支持空格 <BR>支持<STRONG><FONT color=#ff0000>函数</FONT></STRONG>屏蔽功能,在配置文件中可以配置禁止使用的<STRONG><FONT color=#ff0000>函数</FONT></STRONG>列表 <BR>支持变量缓存功能,重复变量字串不多次解析<BR><BR>使用例子:
<DIV style="MARGIN: 6px 0px"><B>代码</B>: <A href="javascript:copycode($('code0'));"><FONT color=#0000ff>复制内容到剪贴板</FONT></A><CODE id=code0><FONT face=NSimsun> </FONT>
<DIV class=code>
<OL>
<LI><SPAN style="COLOR: #007700">{</SPAN><SPAN style="COLOR: #0000bb">$webTitle</SPAN><SPAN style="COLOR: #007700">|</SPAN><SPAN style="COLOR: #0000bb">md5</SPAN><SPAN style="COLOR: #007700">|&nbsp;</SPAN><SPAN style="COLOR: #0000bb">strtoupper&nbsp;</SPAN><SPAN style="COLOR: #007700">|&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000bb">substr</SPAN><SPAN style="COLOR: #007700">=</SPAN><SPAN style="COLOR: #0000bb">0</SPAN><SPAN style="COLOR: #007700">,</SPAN><SPAN style="COLOR: #0000bb">3&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #007700">}<BR>
<LI>{</SPAN><SPAN style="COLOR: #0000bb">$number</SPAN><SPAN style="COLOR: #007700">|</SPAN><SPAN style="COLOR: #0000bb">number_format</SPAN><SPAN style="COLOR: #007700">=</SPAN><SPAN style="COLOR: #0000bb">2&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #007700">}</SPAN><SPAN style="COLOR: #ff8000"></SPAN></LI></OL></DIV></CODE></DIV>如果在应用<SPAN class=t_tag onclick=tagshow(event) href="tag.php?name=Action">Action</SPAN>中需要输出某个变量,使用下面的方法:
<DIV style="MARGIN: 6px 0px"><B>代码</B>: <A href="javascript:copycode($('code1'));"><FONT color=#0000ff>复制内容到剪贴板</FONT></A><CODE id=code1><FONT face=NSimsun> </FONT>
<DIV class=code>
<OL>
<LI><SPAN style="COLOR: #0000bb">$this</SPAN><SPAN style="COLOR: #007700">-&gt;</SPAN><SPAN style="COLOR: #0000bb">assign</SPAN><SPAN style="COLOR: #007700">(</SPAN><SPAN style="COLOR: #dd0000">'name'</SPAN><SPAN style="COLOR: #007700">,</SPAN><SPAN style="COLOR: #dd0000">'value'</SPAN><SPAN style="COLOR: #007700">);</SPAN><SPAN style="COLOR: #ff8000"></SPAN></LI></OL></DIV></CODE></DIV>系统只会输出设定的变量,其它变量不会输出,一定程度上保证了变量的安全性。<BR><BR>系统支持输出数组和对象属性,无论要输出的模板变量是数组还是对象,都可以用下列方式输出:
<DIV style="MARGIN: 6px 0px"><B>代码</B>: <A href="javascript:copycode($('code2'));"><FONT color=#0000ff>复制内容到剪贴板</FONT></A><CODE id=code2><FONT face=NSimsun> </FONT>
<DIV class=code>
<OL>
<LI><SPAN style="COLOR: #007700">{</SPAN><SPAN style="COLOR: #0000bb">$user</SPAN><SPAN style="COLOR: #007700">.</SPAN><SPAN style="COLOR: #0000bb">name</SPAN><SPAN style="COLOR: #007700">}</SPAN><SPAN style="COLOR: #ff8000"></SPAN></LI></OL></DIV></CODE></DIV>系统会自动判断要输出的变量,如果是多维数组或者多层对象属性的输出,请使用下面的定义方式:
<DIV style="MARGIN: 6px 0px"><B>代码</B>: <A href="javascript:copycode($('code3'));"><FONT color=#0000ff>复制内容到剪贴板</FONT></A><CODE id=code3><FONT face=NSimsun> </FONT>
<DIV class=code>
<OL>
<LI><SPAN style="COLOR: #007700">{</SPAN><SPAN style="COLOR: #0000bb">$user</SPAN><SPAN style="COLOR: #007700">[</SPAN><SPAN style="COLOR: #dd0000">'sub'</SPAN><SPAN style="COLOR: #007700">][</SPAN><SPAN style="COLOR: #dd0000">'name'</SPAN><SPAN style="COLOR: #007700">]}<BR>
<LI>{</SPAN><SPAN style="COLOR: #0000bb">$user</SPAN><SPAN style="COLOR: #007700">:</SPAN><SPAN style="COLOR: #0000bb">sub</SPAN><SPAN style="COLOR: #007700">:</SPAN><SPAN style="COLOR: #0000bb">name</SPAN><SPAN style="COLOR: #007700">}</SPAN><SPAN style="COLOR: #ff8000"></SPAN></LI></OL></DIV></CODE></DIV>如果要同时输出多个模板变量,可以使用下面的方式:
<DIV style="MARGIN: 6px 0px"><B>代码</B>: <A href="javascript:copycode($('code4'));"><FONT color=#0000ff>复制内容到剪贴板</FONT></A><CODE id=code4><FONT face=NSimsun> </FONT>
<DIV class=code>
<OL>
<LI><SPAN style="COLOR: #0000bb">$array&nbsp;</SPAN><SPAN style="COLOR: #007700">=&nbsp;array();<BR>
<LI></SPAN><SPAN style="COLOR: #0000bb">$array</SPAN><SPAN style="COLOR: #007700">[</SPAN><SPAN style="COLOR: #dd0000">'name'</SPAN><SPAN style="COLOR: #007700">]&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #dd0000">'thinkphp'</SPAN><SPAN style="COLOR: #007700">;<BR>
<LI></SPAN><SPAN style="COLOR: #0000bb">$array</SPAN><SPAN style="COLOR: #007700">[</SPAN><SPAN style="COLOR: #dd0000">'email'</SPAN><SPAN style="COLOR: #007700">]&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #dd0000">'liu21st@gmail.com'</SPAN><SPAN style="COLOR: #007700">;<BR>
<LI></SPAN><SPAN style="COLOR: #0000bb">$array</SPAN><SPAN style="COLOR: #007700">[</SPAN><SPAN style="COLOR: #dd0000">'phone'</SPAN><SPAN style="COLOR: #007700">]&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #dd0000">'12335678'</SPAN><SPAN style="COLOR: #007700">;<BR>
<LI></SPAN><SPAN style="COLOR: #0000bb">$this</SPAN><SPAN style="COLOR: #007700">-&gt;</SPAN><SPAN style="COLOR: #0000bb">assign</SPAN><SPAN style="COLOR: #007700">(</SPAN><SPAN style="COLOR: #0000bb">$array</SPAN><SPAN style="COLOR: #007700">);</SPAN><SPAN style="COLOR: #ff8000"></SPAN></LI></OL></DIV></CODE></DIV>这样,就可以在模板文件中同时输出name、email和phone三个变量。<BR><BR>除了常规变量的输出外,模板引擎还支持系统变量和系统常量、以及系统特殊变量的输出。它们的输出不需要对模板变量赋值。<BR><BR>系统变量(依然支持<STRONG><FONT color=#ff0000>函数</FONT></STRONG>使用和大小写、空格),以Think.打头,如
<DIV style="MARGIN: 6px 0px"><B>代码</B>: <A href="javascript:copycode($('code5'));"><FONT color=#0000ff>复制内容到剪贴板</FONT></A><CODE id=code5><FONT face=NSimsun> </FONT>
<DIV class=code>
<OL>
<LI><SPAN style="COLOR: #007700">{</SPAN><SPAN style="COLOR: #0000bb">$Think</SPAN><SPAN style="COLOR: #007700">.</SPAN><SPAN style="COLOR: #0000bb">server</SPAN><SPAN style="COLOR: #007700">.</SPAN><SPAN style="COLOR: #0000bb">script_name&nbsp;</SPAN><SPAN style="COLOR: #007700">}&nbsp;</SPAN><SPAN style="COLOR: #ff8000">//取得$_SERVER变量<BR>
<LI></SPAN><SPAN style="COLOR: #007700">{</SPAN><SPAN style="COLOR: #0000bb">$Think</SPAN><SPAN style="COLOR: #007700">.</SPAN><SPAN style="COLOR: #0000bb">session</SPAN><SPAN style="COLOR: #007700">.</SPAN><SPAN style="COLOR: #0000bb">session_id</SPAN><SPAN style="COLOR: #007700">|</SPAN><SPAN style="COLOR: #0000bb">md5&nbsp;</SPAN><SPAN style="COLOR: #007700">}&nbsp;</SPAN><SPAN style="COLOR: #ff8000">//&nbsp;获取$_SESSION变量<BR>
<LI></SPAN><SPAN style="COLOR: #007700">{</SPAN><SPAN style="COLOR: #0000bb">$Think</SPAN><SPAN style="COLOR: #007700">.</SPAN><SPAN style="COLOR: #0000bb">get</SPAN><SPAN style="COLOR: #007700">.</SPAN><SPAN style="COLOR: #0000bb">pageNumber&nbsp;</SPAN><SPAN style="COLOR: #007700">}&nbsp;</SPAN><SPAN style="COLOR: #ff8000">//获取$_GET变量<BR>
<LI></SPAN><SPAN style="COLOR: #007700">{</SPAN><SPAN style="COLOR: #0000bb">$Think</SPAN><SPAN style="COLOR: #007700">.</SPAN><SPAN style="COLOR: #0000bb">cookie</SPAN><SPAN style="COLOR: #007700">.</SPAN><SPAN style="COLOR: #0000bb">name&nbsp;</SPAN><SPAN style="COLOR: #007700">}&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #ff8000">//获取$_COOKIE变量</SPAN></LI></OL></DIV></CODE></DIV>系统常量
<DIV style="MARGIN: 6px 0px"><B>代码</B>: <A href="javascript:copycode($('code6'));"><FONT color=#0000ff>复制内容到剪贴板</FONT></A><CODE id=code6><FONT face=NSimsun> </FONT>
<DIV class=code>
<OL>
<LI><SPAN style="COLOR: #007700">{</SPAN><SPAN style="COLOR: #0000bb">$Think</SPAN><SPAN style="COLOR: #007700">.const.</SPAN><SPAN style="COLOR: #0000bb">__FILE__&nbsp;</SPAN><SPAN style="COLOR: #007700">}&nbsp;<BR>
<LI>{</SPAN><SPAN style="COLOR: #0000bb">$Think</SPAN><SPAN style="COLOR: #007700">.const.</SPAN><SPAN style="COLOR: #0000bb">MODULE_NAME&nbsp;</SPAN><SPAN style="COLOR: #007700">}</SPAN><SPAN style="COLOR: #ff8000"></SPAN></LI></OL></DIV></CODE></DIV>特殊变量 ,由ThinkPHP系统定义的常量
<DIV style="MARGIN: 6px 0px"><B>代码</B>: <A href="javascript:copycode($('code7'));"><FONT color=#0000ff>复制内容到剪贴板</FONT></A><CODE id=code7><FONT face=NSimsun> </FONT>
<DIV class=code>
<OL>
<LI><SPAN style="COLOR: #007700">{</SPAN><SPAN style="COLOR: #0000bb">$Think</SPAN><SPAN style="COLOR: #007700">.</SPAN><SPAN style="COLOR: #0000bb">version&nbsp;</SPAN><SPAN style="COLOR: #007700">}&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #ff8000">//版本&nbsp;<BR>
<LI></SPAN><SPAN style="COLOR: #007700">{</SPAN><SPAN style="COLOR: #0000bb">$Think</SPAN><SPAN style="COLOR: #007700">.</SPAN><SPAN style="COLOR: #0000bb">now&nbsp;</SPAN><SPAN style="COLOR: #007700">}&nbsp;</SPAN><SPAN style="COLOR: #ff8000">//现在时间&nbsp;<BR>
<LI></SPAN><SPAN style="COLOR: #007700">{</SPAN><SPAN style="COLOR: #0000bb">$Think</SPAN><SPAN style="COLOR: #007700">.</SPAN><SPAN style="COLOR: #0000bb">template</SPAN><SPAN style="COLOR: #007700">|</SPAN><SPAN style="COLOR: #0000bb">basename&nbsp;</SPAN><SPAN style="COLOR: #007700">}&nbsp;</SPAN><SPAN style="COLOR: #ff8000">//模板页面&nbsp;<BR>
<LI></SPAN><SPAN style="COLOR: #007700">{</SPAN><SPAN style="COLOR: #0000bb">$Think</SPAN><SPAN style="COLOR: #007700">.</SPAN><SPAN style="COLOR: #0000bb">LDELIM&nbsp;</SPAN><SPAN style="COLOR: #007700">}&nbsp;</SPAN><SPAN style="COLOR: #ff8000">//模板标签起始符号&nbsp;<BR>
<LI></SPAN><SPAN style="COLOR: #007700">{</SPAN><SPAN style="COLOR: #0000bb">$Think</SPAN><SPAN style="COLOR: #007700">.</SPAN><SPAN style="COLOR: #0000bb">RDELIM&nbsp;</SPAN><SPAN style="COLOR: #007700">}&nbsp;</SPAN><SPAN style="COLOR: #ff8000">//模板标签结束符号</SPAN></LI></OL></DIV></CODE></DIV>配置<STRONG><FONT color=#ff0000>参数</FONT></STRONG>
<DIV style="MARGIN: 6px 0px"><B>代码</B>: <A href="javascript:copycode($('code8'));"><FONT color=#0000ff>复制内容到剪贴板</FONT></A><CODE id=code8><FONT face=NSimsun> </FONT>
<DIV class=code>
<OL>
<LI><SPAN style="COLOR: #007700">{</SPAN><SPAN style="COLOR: #0000bb">$Think</SPAN><SPAN style="COLOR: #007700">.</SPAN><SPAN style="COLOR: #0000bb">config</SPAN><SPAN style="COLOR: #007700">.</SPAN><SPAN style="COLOR: #0000bb">db_charset</SPAN><SPAN style="COLOR: #007700">}</SPAN><SPAN style="COLOR: #ff8000"></SPAN></LI></OL></DIV></CODE></DIV>语言变量
<DIV style="MARGIN: 6px 0px"><B>代码</B>: <A href="javascript:copycode($('code9'));"><FONT color=#0000ff>复制内容到剪贴板</FONT></A><CODE id=code9><FONT face=NSimsun> </FONT>
<DIV class=code>
<OL>
<LI><SPAN style="COLOR: #007700">{</SPAN><SPAN style="COLOR: #0000bb">$Think</SPAN><SPAN style="COLOR: #007700">.</SPAN><SPAN style="COLOR: #0000bb">lang</SPAN><SPAN style="COLOR: #007700">.</SPAN><SPAN style="COLOR: #0000bb">page_error</SPAN><SPAN style="COLOR: #007700">}</SPAN><SPAN style="COLOR: #ff8000"></SPAN></LI></OL></DIV></CODE></DIV>还有一些常用的变量输出快捷标签,包括下面:
<DIV style="MARGIN: 6px 0px"><B>代码</B>: <A href="javascript:copycode($('code10'));"><FONT color=#0000ff>复制内容到剪贴板</FONT></A><CODE id=code10><FONT face=NSimsun> </FONT>
<DIV class=code>
<OL>
<LI><SPAN style="COLOR: #007700">{:function(</SPAN><SPAN style="COLOR: #0000bb">…</SPAN><SPAN style="COLOR: #007700">)}&nbsp;</SPAN><SPAN style="COLOR: #ff8000">//执行方法并输出返回值<BR>
<LI></SPAN><SPAN style="COLOR: #007700">{~function}&nbsp;</SPAN><SPAN style="COLOR: #ff8000">//执行方法不输出<BR>
<LI></SPAN><SPAN style="COLOR: #007700">{@var}&nbsp;</SPAN><SPAN style="COLOR: #ff8000">//输出Session变量<BR>
<LI></SPAN><SPAN style="COLOR: #007700">{&amp;var}&nbsp;</SPAN><SPAN style="COLOR: #ff8000">//输出配置<STRONG><FONT color=#ff0000>参数</FONT></STRONG><BR>
<LI></SPAN><SPAN style="COLOR: #007700">{%var}&nbsp;</SPAN><SPAN style="COLOR: #ff8000">//输出语言变量<BR>
<LI></SPAN><SPAN style="COLOR: #007700">{.var}&nbsp;</SPAN><SPAN style="COLOR: #ff8000">//输出GET变量<BR>
<LI></SPAN><SPAN style="COLOR: #007700">{^var}&nbsp;</SPAN><SPAN style="COLOR: #ff8000">//输出POST变量<BR>
<LI></SPAN><SPAN style="COLOR: #007700">{*var}&nbsp;</SPAN><SPAN style="COLOR: #ff8000">//输出常量</SPAN></LI></OL></DIV></CODE></DIV>

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.