Search code examples
smartysmarty3cmsmadesimple

placing Div and UL element inside of foreach loop


I'm working with CMSMS 1.11.11, which has Smarty 3 built into it. I have the following code, which allows me to detect if a news field definitions exist....

{if isset($entry->fields)} 
<div class="flexslider">
<ul class="slides">
{foreach from=$entry->fields item='field'}
{if $field->type == 'file'}
{if $field->name|strpos:"Photo" !== false}
{if $field->displayvalue != ''}
<li><img src="{$entry->file_location}/{$field->displayvalue}"/></li>
{/if}
{/if}
{/if}
{/foreach}
</ul>
</div>
{/if}

...and if they do exist, then the foreach loop will loop each item into an <li>

My question is if it is possible to place the <div class="flexslider"> and <ul class="slides"> inside the foreach loop still get the same effect? I basically would like everything packaged in the foreach loop, but only have the li's continue to loop and increase, while the outer <ul> and outter <div> just loads once if any of the condition is met.

Is this possible?


Solution

  • You can put div and ul inside foreach but you still need if condition here:

    {if isset($entry->fields)} 
    {foreach $entry->fields as $field}
    {if $field@first}
    <div class="flexslider">
    <ul class="slides">
    {/if}
    {if $field->type == 'file'}
    {if $field->name|strpos:"Photo" !== false}
    {if $field->displayvalue != ''}
    <li><img src="{$entry->file_location}/{$field->displayvalue}"/></li>
    {/if}
    {/if}
    {/if}
    {if $field@last}
    </ul>
    </div>
    {/if}
    {/foreach}
    {/if}
    

    I've changed also loop style from Smarty2 to Smarty3