【问题标题】:Access smarty loop property in included file?在包含的文件中访问 smarty 循环属性?
【发布时间】:2013-08-18 07:01:28
【问题描述】:

使用 smarty 时,它会在循环键上提供一些有用的 @properties,例如 @first @last@iteration

但似乎在循环中看起来像

{foreach $collection as $item}
    {include file="something_else.tpl"}
{/foreach}

我们似乎失去了@properties

如果我想访问这些属性,有没有比将所有属性传递给包含文件更优雅的方法?

{foreach $collection as $item}
    {include file="something_else.tpl" first=$item@first last=$item@last iter...}
{/foreach}

【问题讨论】:

    标签: php properties foreach include smarty


    【解决方案1】:

    只要您在循环中使用@properties,它们就会被计算出来,并且在包含的文件中也可以使用:

    {foreach $collection as $item}
        {if $item@first || $item@last || $item@iteration}{/if} {* Force @properties to be calculated so they can be used in included files *}
        {include file="something_else.tpl"}
    {/foreach}
    

    似乎@properties 事件不必在模板逻辑中使用,但如果它们在评论中就足够了。然后,您可以避免在编译的模板文件中出现空的 if 条件。以下目前有效,但我不确定它是否是预期的。

    {foreach $collection as $item}
        {* $item@first $item@last $item@iteration Force @properties to be calculated so they can be used in included files *}
        {include file="something_else.tpl"}
    {/foreach}
    

    【讨论】:

      【解决方案2】:

      基本上没有。 但也许您可以使用混合语法。您向 foreach 添加名称并尝试通过 {$smarty.foreach.name.property} 访问其属性。

      http://www.smarty.net/docs/en/language.variables.smarty.tpl (搜索 $smarty.foreach。)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-17
        • 1970-01-01
        • 1970-01-01
        • 2014-03-01
        • 2011-12-17
        相关资源
        最近更新 更多