访问包含文件中的智能循环属性


Access smarty loop property in included file?

使用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}

只要在循环中使用@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}

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

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