>php教程 >php手册 >Smarty在模板文件中的一些应用

Smarty在模板文件中的一些应用

WBOY
WBOY원래의
2016-06-13 11:38:42773검색

1 数学运算可以直接应用于模版标记中的变量

<ol class="dp-c">
<li class="alt"><span><span>{</span><span class="vars">$foo</span><span>+1}   </span></span></li>
<li>
<span>{</span><span class="vars">$foo</span><span>*</span><span class="vars">$bar</span><span>}   </span>
</li>
<li class="alt">
<span>{</span><span class="keyword">if</span><span> (</span><span class="vars">$foo</span><span>+</span><span class="vars">$bar</span><span>%</span><span class="vars">$baz</span><span>*134232+10+</span><span class="vars">$b</span><span>+10)}...{/</span><span class="keyword">if</span><span>} </span>
</li>
</ol>

2 GET,POST,SERVER,SESSIOM,COOKIES等页面请求变量,可以在模板中直接访问

<ol class="dp-c">
<li class="alt"><span><span>{*显示来自URL的GET传值</span><span class="string">"page"</span><span> *}   </span></span></li>
<li>
<span>{</span><span class="vars">$smarty</span><span>.get.page}   </span>
</li>
<li class="alt"><span> </span></li>
<li>
<span>{*显示来自一个表单(POST )的变量</span><span class="string">"page"</span><span> *}   </span>
</li>
<li class="alt">
<span>{</span><span class="vars">$smarty</span><span>.post.page}   </span>
</li>
<li><span> </span></li>
<li class="alt">
<span>{*显示cookie </span><span class="string">"username"</span><span>的值 *}   </span>
</li>
<li>
<span>{</span><span class="vars">$smarty</span><span>.cookies.username}   </span>
</li>
<li class="alt"><span> </span></li>
<li>
<span>{*显示 server 变量</span><span class="string">"SERVER_NAME"</span><span> *}   </span>
</li>
<li class="alt">
<span>{</span><span class="vars">$smarty</span><span>.server.SERVER_NAME}   </span>
</li>
<li><span> </span></li>
<li class="alt">
<span>{*显示系统环境(env)的变量</span><span class="string">"PATH"</span><span> *}   </span>
</li>
<li>
<span>{</span><span class="vars">$smarty</span><span>.env.PATH}   </span>
</li>
<li class="alt"><span> </span></li>
<li>
<span>{*显示PHP session 变量</span><span class="string">"id"</span><span> *}   </span>
</li>
<li class="alt">
<span>{</span><span class="vars">$smarty</span><span>.session.id}   </span>
</li>
<li><span> </span></li>
<li class="alt">
<span>{*显示来自get/post/cookies/server/env 的变量</span><span class="string">"username"</span><span> *}   </span>
</li>
<li>
<span>{</span><span class="vars">$smarty</span><span>.request.username} </span>
</li>
</ol>

 3 可以通过{$smarty.now}来访问当前的时间戳(timestamp),可以通过date_format 变量调节器来为特定的输出作处理

<ol class="dp-c">
<li class="alt"><span><span>{</span><span class="vars">$smarty</span><span>.now|date_format:</span><span class="string">"%Y-%m-%d %H:%M:%S"</span><span>}   </span></span></li>
<li><span>{*这里用到了Smarty变量修饰符*} </span></li>
</ol>

4 可以通过{$smarty.const}来直接访问 PHP 常量

<ol class="dp-c"><li class="alt"><span><span>{</span><span class="vars">$smarty</span><span>.</span><span class="keyword">const</span><span>.My_Val} </span></span></li></ol>
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.