Smarty current on associative array

I have an associative array and I'm trying to get the first record with smarty.

In php, I use current (array), but smarty dont seems to have current.

So I wrote the code below

{if is_array($browseProducts.data) }
    <ul class="products-grid">

        {foreach from=$browseProducts.data item=item}
            {assign var='image' value=''}
            {if is_array($item.images) }
                {php} $image=current($item.images); {/php} 
            {/if}   
        {/foreach}

    </ul>
{/if}

in the field {php} current ($ item.images) gives a warning: current () [function.current]: the passed variable is not an array or object

The syntax is right, so I think smarty's $ item.images cannot be read {php}

Any way to pass $ item.images to the {php} section or whatever.

Any suggestion to solve my problem?

+3
source share
2 answers
{$item.images.0}

should return the first element of the $ item.images array.

So:

{if is_array($browseProducts.data) }
    <ul class="products-grid">

        {foreach from=$browseProducts.data item=item}
            {assign var='image' value=''}
            {if is_array($item.images) }
                {$item.images.0}
            {/if}   
        {/foreach}

    </ul>
{/if}
+2
source

But what if array keys are associative? You could also:

{$item.images|@current}
+12
source

Source: https://habr.com/ru/post/1768010/


All Articles