Home > Article > Backend Development > Some applications of Smarty in template files_PHP tutorial
1 Mathematical operations can be applied directly to variables in template tags
<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 and other page request variables can be accessed directly in the template
<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 The current timestamp can be accessed through {$smarty.now}, and specific output can be processed through the date_format variable regulator
<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 You can directly access PHP constants through {$smarty.const}
<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>