0

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}
4

2 に答える 2

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}

テンプレートロジックでdon'tイベントを使わないといけないようです@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}
于 2018-01-17T11:11:00.907 に答える
0

基本的にいいえ。ただし、混合構文を使用できる場合があります。foreach に名前を追加し、{$smarty.foreach.name.property} を介してそのプロパティにアクセスしようとします。

http://www.smarty.net/docs/en/language.variables.smarty.tpl ($smarty.foreach を検索してください。)

于 2013-10-06T13:58:28.113 に答える