【问题标题】:smarty name of variable dynamic变量 dynamic 的 smarty 名称
【发布时间】:2013-09-23 22:44:11
【问题描述】:

我学习 smarty 大约两个月了,我发现自己遇到了一个问题。

我正在创建一个从名为 submenu.conf 的文件中读取配置的动态菜单,该文件是:

tot_sub_menu_2 = "2"
text_sub_menu_2_1 = "Home"
text_sub_menu_2_2 = "about"

我的应用程序中的 tpl 文件我希望他能以这种方式动态创建菜单:

{for $foo=1 to #tot_sub_menu_2#}

<li><a href="{#text_sub_menu_2_.{$foo}}">{#text_sub_menu_2_.{$foo}}</a></li>
{/for}

我想取一个动态参数 text_sub_menu_2_1 第二个循环变成 text_sub_menu_2_2 等等。

有人知道如何帮助我吗?

【问题讨论】:

    标签: templates smarty variable-assignment template-engine


    【解决方案1】:

    我还没有测试,但你可以尝试使用{counter}

    {for $foo=1 to #tot_sub_menu_2#}
    {* initialize the count *}
    {counter start=0 skip=1}
    <li><a href="{#text_sub_menu_2_.{counter}}">{#text_sub_menu_2_.{counter}}</a></li>
    

    否则,您可以使用@iteration 属性。见http://www.smarty.net/docs/en/language.function.foreach.tpl#foreach.property.iteration

    iteration 包含当前循环迭代并且总是从 一,不同于索引。 每次迭代都加一

    {foreach from=$new_products item='product' name='newProducts'} 
    // Do somthing 
    
    //Here i want to increment {$count} if it's lower than 3 or set to 1 if higher than 3 
    {if $product@iteration <= 3}{$count =  $product@iteration}{else}{$count = 1}{/if} 
    
    {/foreach}
    

    【讨论】:

      猜你喜欢
      • 2012-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-25
      相关资源
      最近更新 更多