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

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-13 11:38:42781Durchsuche

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>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn