dongyun234854 2013-10-29 09:54 采纳率: 0%
浏览 14

使用Smarty 2在一行上分配变量值

I am using Smarty 2 and wondered if there is a better/tidier way to use assign to set the value of evenRow on one line, rather than the 5 lines below.

{if $evenRow == 'on'}
   {assign var='evenRow' value='off'}
{else}
   {assign var='evenRow' value='on'}
{/if}

Considering Smarty can be quite closely aligned with PHP in places i'm surprised this isn't easily found online, as doing something like this in PHP would be straightforward.

  • 写回答

3条回答 默认 最新

  • duanji1899 2013-10-29 15:44
    关注

    From Smarty documentation (Smarty 3):

    Although Smarty can handle some very complex expressions and syntax, it is a good rule of thumb to keep the template syntax minimal and focused on presentation. If you find your template syntax getting too complex, it may be a good idea to move the bits that do not deal explicitly with presentation to PHP by way of plugins or modifiers.

    What they are basically suggesting is that you should keep the amount of logic in the templates down and use functions or modifiers instead. For simple cases you can use simple expressions as attribute values:

    {assign var=test_var value=!empty($some_input)}
    

    For more complex examples, you can write your own modifier:

    function smarty_modifier_do_something_complex($input) {
      // Process input and return value
    }
    

    and use it like this:

    {assign var=test_var value=$some_input|do_something_complex}
    

    Or you can stick with a more verbose {if} … {else} … {/if} approach.

    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集