Construction is this:
<!-- projects list -->
{if !empty($userObjects)}
<select id="projects-list" tabindex="1" name="project">
{if !isset($selected)}<option value="0">Choose project</option>{/if}
{foreach from=$userObjects item=v}
<option value="{$v.Id}" {if $selected==$v.Id}selected="selected"{/if} }>{$v.Name}
{* if it's 1st element *}
{if $smarty.foreach.v.index == 0}
{if isset($limit)}<br /><span id="projlimit">{$limit}</span> {$currency->sign}{/if}
{/if}
</option>
{/foreach}
</select>
as you can see I did
{if $smarty.foreach.v.index == 0}
but it's going wrong. In this case all the options elemets has a $limit value. How to make it good? I need only first one.
I don't want to appear rude, but Bondye's answer will not work in all cases. Since PHP's arrays are ordered maps, the value of the first key will not always be 0.
In these cases you can use the #index, #iteration or #first properties. More details are in the smarty foreach documentation at http://www.smarty.net/docs/en/language.function.foreach.tpl#foreach.property.iteration
One of the possible solutions to your question is bellow:
{foreach $rows as $row}
{if $row#iteration == 1}
First item in my array
{/if}
{/foreach}
You can use this code:
{foreach from=$userObjects item=v name=obj}
{if $smarty.foreach.obj.first}
This is the first item
{/if}
{if $smarty.foreach.obj.last}
This is the last item.
{/if}
{/foreach}
Could you do this by the array key?
{foreach from=$rows key=i item=row}
{if $i == 0}
First item in my array
{/if}
{/foreach}
Related
I have smarty value if it empty need another result
{get_video_meta video_id=$video_data.id key='test'}
This value print "test" If this value is empty else how can I use?
{if !empty({get_video_meta video_id=$video_data.id key='test'})}
No value
{else}
{get_video_meta video_id=$video_data.id key='test'}
{/if}
This code not work
You could use the {capture} function to capture the output into a variable, then choose what to do with it:
{capture name="video_meta"}{get_video_meta video_id=$video_data.id key='test'}{/capture}
{if empty($smarty.capture.video_meta)}
No value
{else}
{$smarty.capture.video_meta}
{/if}
You can assign value to variable:
{if empty($code)}
{assign var='code' value='404'}
{/if}
but i suppose that you need something like:
{if empty($video_data.id)}
No value
{else}
{get_video_meta video_id=$video_data.id key='test'}
{/if}
because get_video_meta - it's smarty plugin, and you can't use it like you try to use...
In foreach:
{foreach from=$video_data key=k item=v}
{if empty($k)}
No value
{else}
{get_video_meta video_id=$v.id key=$k}
{/if}
{/foreach}
The variable I am stating with is a string of HTML code. I am trying to explode on "line break", then explode the result on a "|".
here is the code I am using
{assign var="featuresdescarray" value="<br />"|explode:$product.featuresdesc}
{foreach from=$featuresdescarray key=k item=i}
{assign var="featuresdescarrayexploded" value="|"|explode:$i}
{foreach from=$featuresdescarrayexploded key=key item=item}
{if $item eq "yes"}
yes-textreplaced
{elseif $item eq "no"}
no-textreplaced
{else}
{$item}<br />
{/if}
{/foreach}
{/foreach}
So the fist time through the nested foreach the text gets replaced as I expect. after that it seems to fail the if, elseif and just use the else every time.
If i take out the if statement and put in a {$key} : {$item} line it looks like it should work.
Not sure what I am missing
OK I am not sure why this works and the original code does not but here is what is working for anyone trying something like this.
I took out the yes and no and replaced it with 1 and 0
{assign var="featuresdescarray" value="<br />"|explode:$product.featuresdesc}
{foreach from=$featuresdescarray key=k item=i}
{if $i|strstr:"|"}
{assign var="featuresdescarrayexploded" value="|"|explode:$i}
{foreach from=$featuresdescarrayexploded key=key item=item}
{if $key eq 0 and $item eq 1}
<div class="productFeatureDescYes">{$featuresdescarrayexploded[1]}<br /></div>
{elseif $key eq 0 and $item eq 0}
<div class="productFeatureDescNo">{$featuresdescarrayexploded[1]}<br /></div>
{/if}
{/foreach}
{else}
{$i}<br />
{/if}
{/foreach}
I added a if statement to search the variable $i for a "|" if it has one then it is exploded and wrapped in the proper div. if not it is just displayed (to be wrapped in a div later).
I also run the if statement on the key[0] section in the second foreach statement.
The danger of self taught coding i guess, sometime it is just trial and error but I always like to find out what i did wrong and this time it is not the case.
I am not sure why the 0's and 1's worked better then "no" and "yes" so if anyone can answer the original question that would be great.
Need to delete first element after all this statements, i try to add key=i to second foreach, and add {if $i!=0}, but it dont work for me, because i have multilevel system and after all if statements first $i is different for all pages.
{foreach from=$page.path item=e}
{assign var=element value=0}
{foreach from=$menu3 item=r}
{if $e.page_id==$r.page_id}{assign var=element value=$r}{/if}
{if $element._left < $r._left && $element._right > $r._right}
{if $r._level==$element._level+1 && $r._level==$page._level}
{$r.name}
{/if}
{/if}
{/foreach}
{/foreach}
P.S. Sorry for my English.
Add a name to you foreach and do something like this to get index
{foreach from=$items key=myId item=i name=foo}
{$smarty.foreach.foo.index} <!-- It will show index -->
{/foreach}
I assume you don't want to display first element in each of the menus you have, right?
{foreach from=$page.path item=e}
{assign var=first_skipped value=0}
{assign var=element value=0}
{foreach from=$menu3 item=r}
{if !$first_skipped}
{assign var=first_skipped value=1}
{else}
{if $e.page_id==$r.page_id}{assign var=element value=$r}{/if}
{if $element._left < $r._left && $element._right > $r._right}
{if $r._level==$element._level+1 && $r._level==$page._level}
{$r.name}
{/if}
{/if}
{/if}
{/foreach}
{/foreach}
<div id="favorite-first" class="">
{foreach from=$arrSection key=k item=v}
{if $k==$selectedSection}
{$v}
{/if}
{/foreach}
</div>
<div id="favorite-toggle"><br></div>
<div id="favorite-inside" class="slideUp">
{foreach from=$arrSection key=k item=v}
{if $k==$selectedSection}
{else}
<div class="favorite-action" id="{$k}">{$v}</div>
{/if}
{/foreach}
</div>
If $arrSection array returns only one value (i.e $k). I need to hide div (favorite-toggle,favorite-inside) How can i do this in smarty
If $arrSection array returns only one value
{if count($arrSection) eq 1}
there is only one item
{else}
there is > one or zero items
{/if}
If you give the "foreach" a name argument you can access certain foreach properties:
{foreach from=$arrSection key=k item=v name=NAME}
Let's say the number of iterations in total:
$smarty.foreach.NAME.total
Then you now how often smarty will loop and if it's in your case only one time.
Im trying to view contents of array on the page:
{foreach from=$entries key=i item=topic}
{if $topic.topic_style == question}
<li class="mail">
{$topic.title}
{$topic.tags}
</li>
{/if}
{/foreach}
$topic.tags is an array but i dont seem to be able to extract the contents to the page can anyone help?
Try this:
{foreach from=$entries key=i item=topic}
{if $topic.topic_style == question}
<li class="mail">
{$topic.title}
{foreach from=$topic.tags key=j item=tag}
{$tag}
{/foreach}
</li>
{/if}
{/foreach}
Where tag is the value of the tag name in the tags array().