PHP中Smarty引擎的常用语法_极客玩家大白

smarty在PHP项目开发中用得挺多的, 主要用于实现前后端分离, 本文来总结一下常见的用法.

输出今天的日期:

1
{$smarty.now|date_format:"%H:%M %A, %B %e, %Y"}

实际上用到了PHP的time()函数

明天Date of Tomorrow:

1
{"tomorrow"|date_format:"%A, %B %e, %Y"}

后天Date of the day after tomorrow (Day+2):

1
{"+2 days"|date_format:"%A, %B %e, %Y"}

(Day+3):

1
{"+3 days"|date_format:"%A, %B %e, %Y"}

相关链接:

How to do to print out date of tomorrow (day+1) with Smarty? - v1.x Store Design & Templates - CS-Cart Community Forums http://forum.cs-cart.com/topic/8070-how-to-do-to-print-out-date-of-tomorrow-day1-with-smarty/

foreach

1
2
3
    {foreach from=$workDetails key=key item=workDetail}
        <td>{$workDetail['name']}</td>
    {/foreach}

if, else

1
2
3
4
5
{if $weekDay neq 0 and  $weekDay neq 6}

{else}

{/if}

调用php函数

{$monthDay|substr:'5':'9'}

1
2
3
4
5
6
7
8
9
{if $weekDay neq 0 and  $weekDay neq 6}
    <th>
        <div style="text-align: center; width:{$tableTdWidth * ($groupMaxCostData['groupMaxCostTimes'][$monthDay] + 1)}px">{$monthDay|substr:'5':'9'}<div>
    </th>
{else}
    <th style="background-color: gray;">
        <div style="width:{$tableTdWidth * ($groupMaxCostData['groupMaxCostTimes'][$monthDay] + 1)}px">{$monthDay|substr:'5':'9'}<div>
    </th>
{/if}

相等与不等

eq、neq 和shell的写法接近

1
$var eq 5

是否有值

1
{if $var}

版权声明

  • 本文作者:极客玩家大白
  • 本文链接:https://geekplayers.com/smarty_usage_in_php.html
  • 郑重声明:本文为博主原创或经授权转载的文章,欢迎转载,但转载文章之后必须在文章页面明显位置注明出处,否则保留追究法律责任的权利。如您有任何疑问或者授权方面的协商,请留言。


一个有故事的程序员

(转载本站文章请注明作者和出处 极客玩家大白

点击了解 :.NET技术人的网站


Show Disqus Comments

Post Directory