Smarty current on associative array - smarty

i have an associative array and i am 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 {php} section current($item.images) gives Warning: current() [function.current]: Passed variable is not an array or object
The syntax is right so i guess the $item.images from smarty can not be read by {php}
Any way to pass $item.images to {php} section, or what.
Any suggestion to solve my problem?

But what if the keys of the array are associative? You could alsdo do:
{$item.images|#current}

{$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}

Related

Smarty foreach / shuffle/random and limit

I want to get items foreach, shuffle them and limit the output to 4 items.
i've this. (displays all items)
{content block_id=27}
{if $_content_27|count >0}
{foreach name=aussen item=box_data from=$_content_27}
<article class="news_row_article col-lg-3">
<div><img src="images/ImgResponsive_Placeholder.png" alt=""/></div>
<h3 class="news_row_headline">{$box_data.content_heading}</h3>
<p class="news_row_content">{$box_data.content_body}</p>
</article>
{/foreach}
{/if}
i've found this
{foreach array_rand($a_firm_display,5) as $i}
<img src="{$a_firm_display[$i].firm_logo}" />
{/foreach}
or this
Generate distinct smarty random numbers
{assign var="distinct_numbers" value=array_fill(1,15,'x')}
{assign var="distinct_numbers" value=array_keys($distinct_numbers)}
{assign var="x" value=shuffle($distinct_numbers)}
{* result *}
{foreach from=$distinct_numbers item="value"}
{$value} |
{/foreach}
hm..somehow get the items from $_content_27 put them into array, output, and limit them ?
any ideas ?
For older version of smarty I found very useful this snippet to shuffle my key => value array
{capture}{$items|#shuffle}{/capture}
Just place it before your foreach loop, I hope it helps somebody.
Maybe you can use shuffle and for instead of foreach, if you don't mind losing the array keys:
{$_content_27=$_content_27|#shuffle}
{for $iter=0 to 3}
...
{$_content_27.$iter.content_heading}
...
{/for}

How to limit the amount of the loop

I am a new guy in smarty.This piece of code will loop all the list,but I just want 10 items, is there anyway to solve this problem in smarty? Thanks!
<ul>
{section name=list loop=$list.list}
<li><span>{$list.list[list].co_title|truncate:20:"...":true}</span></li>
{/section}
</ul>
Set the max attribute:
<ul>
{section name=list loop=$list.list max=10}
<li><span>{$list.list[list].co_title|truncate:20:"...":true}</span></li>
{/section}
</ul>
This and other Smarty section attribute settings can be found in the Smarty Section Docs

How to remove duplicate values on array using Smarty

I have an array that have duplicated values, I want to print the value only once no matter how many time it exist on the array.
This is the code that I have:
{foreach item="item" from=$root.page.news.item }
{foreach from=$item.Tags item=tagitem key=kex}
{$tagitem}
{/foreach}
{/foreach}
This is what it prints right:
kim000kardashian
kim000kardashian
miley000cyrus
miley000cyrus
kim000kardashian
irina000shayk
and this is what I am looking to print
kim000kardashian
miley000cyrus
irina000shayk
Is there a way to achieve this only using Smarty Templates? or any way that I can use on the .tpl files?
Thanks in advance
{foreach item="item" from=$root.page.news.item }
{foreach from=$item.Tags item=tagitem key=kex}
{if !$done.$tagitem}
{$done.$tagitem = 1}
{$tagitem}
{/if}
{/foreach}
{/foreach}
I am not sure it works with all the versions.
Maybe it would be a bit cleaner to call an array_unique() in the php.

How to assign string PHP variable (function) in smarty?

I need to change from section by $type. In from section we have an PHP static functions:
{foreach from=dbPay::getAll(null) item=dbPay}
{foreach from=dbPay::getSomething(null) item=dbPay}
etc.
Let's see, we make an variable for that:
{foreach from={$static_php_function} item=dbPay}
Usage:
{if ($type == 1)}
// need to assign: dbPay::getAll(null)
// ?? {assign var="static_php_function" value="dbPay::getAll(null)"}
{elseif ($type == 2)}
// need to assign: dbPay::getSomething(null)
// ?? {assign var="static_php_function" value="dbPay::getSomething(null)"}
{/if}
{foreach from={$static_php_function} item=dbPay}
...
{/foreach}
Just drop double quotes around function call, and curly braces around variable name in foreach loop:
{if ($type == 1)}
{assign var="static_php_function_result" value=dbPay::getAll(null)}
{elseif ($type == 2)}
{assign var="static_php_function_result" value=dbPay::getSomething(null)}
{/if}
{foreach from=$static_php_function_result item=dbPay}
...
{/foreach}
Remember that it does not assign function for later call, but result to operate on.
simple
add the output of you function depending what type it is in the handler to a var.
add that var to you template.
And fixed.

Counter inside iterate smarty loop

I have this smarty code :
{iterate from=fruits item=fruit}
....
{/iterate}
I want to have a counter inside this loop that accept a start value and increase by one until the loop continues.
I should i use? i am not good in smarty.
Thank you.
You can use the .iteration
{foreach from=fruits item=fruit}
current item #: {$smarty.foreach.fruits.iteration}
{/foreach}
source: http://www.smarty.net/docsv2/en/language.function.foreach.tpl#foreach.property.index
In case you have something like this:
{foreach from=$fruits item=fruit} {/foreach}
Instead of this:
{foreach from=fruits item=fruit} {/foreach}
You should use this syntax:
{foreach from=$fruits item=fruit name=counter}
Iteration no {$smarty.foreach.counter.iteration}
{/foreach}

Resources