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

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-13 11:38:42
Original
784 people have browsed it

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>
Copy after login

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>
Copy after login

 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>
Copy after login

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>
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
smarty turns on caching garbled characters
From 1970-01-01 08:00:00
0
0
0
Why does tp use smarty template? ?
From 1970-01-01 08:00:00
0
0
0
How do you use smarty to integrate laravel5?
From 1970-01-01 08:00:00
0
0
0
Two ways to install smarty
From 1970-01-01 08:00:00
0
0
0
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template