<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.
Related
I'm attempting to create a navigation while using smarty for the first time. I want to display all categories except one which I want to capture and use later on.
I got it to work and get the categories and put them in list. However that one category which is being captured, does not display where I want it to display.
my categories
Soccer //
subCat1 // those categories should be captured
subCat2 //
subCat3 //
Rugby
subCat1
subCat2
subCat3
Netball
subCat1
subCat2
subCat3
etc...
so my code goes like this:
<div>
<!-- deal with category levels -->
{if empty($category_level)}
{assign var="category_level" value=1}
{else}
{math equation="x + 1" x=$category_level assign="category_level"}
{/if}
{assign var="captured" value="false"}
{foreach from=$categories item="category"}
{if (strstr($category.name, 'Soccer') == true)}
{assign var="captured" value="y"}
{assign var="capItem" value="soccerCats"} <!-- used it to see whether the condition was met and this code has been run -- this has been assigned corerctly -->
{capture name="soccerCats" assign="soco"} <!-- start capturing if above condition was met -->
{/if}
{if $category.level == $category_level && $category.is_visible == "Yes"}
<ul>
<li class="{$category.name|htmlspecialchars}Cat">{$category.name|htmlspecialchars}<a class="mobileOnly"><i class="fa fa-angle-down fa-fw"></i></a>
{if !empty($category.children)}
<ul>
{assign var="categories" value=$category.children}
{if empty($category_level)}
{assign var="category_level" value=1}
{else}
{math equation="x + 1" x=$category_level assign="category_level"}
{/if}
{foreach from=$categories item="category"}
{if $category.level == $category_level && $category.is_visible == "Yes"}
<li>{$category.name|htmlspecialchars}</li>
{/if}
{/foreach}
{math equation="x - 1" x=$category_level assign="category_level"}
{assign var="category" value=0}
</ul>
{/if}
</li>
</ul>
{/if}
{if $captured == "y"}
{/capture} <!-- end the capture -->
{assign var="captured" value="n"}
{/if}
{/foreach}
{math equation="x - 1" x=$category_level assign="category_level"}
{assign var="category" value=0}
</div>
<!-- I tried to use both ways shown below to display the captured item -->
{$soco}
{$smarty.capture.soccerCats}
any idea why is this happening?
I don't think {capture} works inside {if} tags, and if does, I'd advise against it, for better code readability. Try something like this:
<div>
<!-- deal with category levels -->
{if empty($category_level)}
{assign var="category_level" value=1}
{else}
{math equation="x + 1" x=$category_level assign="category_level"}
{/if}
{foreach from=$categories item="category"}
{capture name="code"}
<ul>
<li class="{$category.name|htmlspecialchars}Cat">{$category.name|htmlspecialchars}<a class="mobileOnly"><i class="fa fa-angle-down fa-fw"></i></a>
{if !empty($category.children)}
<ul>
{assign var="categories" value=$category.children}
{if empty($category_level)}
{assign var="category_level" value=1}
{else}
{math equation="x + 1" x=$category_level assign="category_level"}
{/if}
{foreach from=$categories item="category"}
{if $category.level == $category_level && $category.is_visible == "Yes"}
<li>{$category.name|htmlspecialchars}</li>
{/if}
{/foreach}
{math equation="x - 1" x=$category_level assign="category_level"}
{assign var="category" value=0}
</ul>
{/if}
</li>
</ul>
{/capture}
{if (strstr($category.name, 'Soccer') != true)}
{$smarty.capture.code}
{else}
{$soccercats=$smarty.capture.code} {*Updated with #Borgtex's comment*}
{/if}
{/foreach}
</div>
{$soccercats}
The idea is that you capture everything and if the category is not 'soccer', you display the captured data; otherwise, you assign it to another capture, to use it in the end, outside the loop. The code assumes that there is a single soccer category, as you initially stated in your question.
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}
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}
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().