Search code examples
smartysmarty3

Why is my nested IF not working in Smarty when using first and last properties?


I'm trying to create a combination of names with a first and a last property. This works, but when i nest it into another if it goes broken.

Regular code:

{{foreach from=$xxx.aaaa item='aaaa_item' name=members}}    

        {{if $smarty.foreach.members.last}}
         & {{$aaaa.bbbb}}
        {{elseif $smarty.foreach.members.first}}
        {{$aaaa.bbbb}}
        {{else}}
        , {{$aaaa.bbbb}}
        {{/if}}
     {{/foreach}}

Results

name, name, name & name

Nested code:

{{foreach from=$xxx.aaaa item='aaaa_item' name=members}} 
    {{if $aaaa_item.cccc == "no"}}

        {{if $smarty.foreach.members.last}}
         & {{$aaaa.bbbb}}
        {{elseif $smarty.foreach.members.first}}
        {{$aaaa.bbbb}}
        {{else}}
        , {{$aaaa.bbbb}}
        {{/if}}


    {{/if}}
{{/foreach}}

Results

,name, name, name

So it only give the result of the final 'else'

Could anyone please help me?


Solution

  • Just found the solution actually:

    {{foreach from=$xxx.aaaa item=aaaa' name=members}}
        {{if $aaaa_item.cccc == "no"}}      
            {{if $smarty.foreach.members.first}}{{$aaaa.bbbb}}
            {{elseif $smarty.foreach.members.last}} & {{$aaaa.bbbb}}
            {{elseif $smarty.foreach.members}}, {{$aaaa.bbbb}}
            {{/if}}
        {{/if}}
    {{/foreach}}