【问题标题】:Is there a way to assign an object to a variable in Smarty 3?有没有办法将对象分配给 Smarty 3 中的变量?
【发布时间】:2013-07-01 06:07:17
【问题描述】:

我正在升级一个使用 Smarty 2 和 Smarty 3 的应用程序,我想在模板中将一个对象分配给一个变量名。原代码:

{section name=articles loop=$list_article}
  {assign var="article" value="`$list_article[articles]`"}

  // now use many properties of the object $article...
  <h2>{$article->title}</h2>
  {$article->text}
  ...
{/section}

但这不适用于 Smarty 3,似乎 {assign} 只能分配字符串。它与 Smarty 2 一起使用。Smarty 3 有替代语法吗?

【问题讨论】:

    标签: smarty smarty3


    【解决方案1】:

    避开引号,直接看值。

    {section name=articles loop=$list_article}
      {assign var="article" value=$list_article[articles]}
    
      // now use many properties of the object $article...
      <h2>{$article->title}</h2>
      {$article->text}
      ...
    {/section}
    

    不过,在这种情况下,您也可以使用 foreach。

    {foreach from=$list_article item=article}
      // now use many properties of the object $article...
      <h2>{$article->title}</h2>
      {$article->text}
      ...
    {/section}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-16
      • 1970-01-01
      • 1970-01-01
      • 2015-03-20
      • 2021-08-22
      • 2019-04-04
      • 2021-08-12
      • 2012-04-24
      相关资源
      最近更新 更多