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().
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.
I'm using CS-Cart 4.1.x
I created an override for the block "design\themes\basic\templates\blocks\products\products_text_links.tpl"
The code that I see in the default file is:
{** block-description:text_links **}
<{if $block.properties.item_number == "Y"}ol{else}ul{/if} class="bullets-list">
{foreach from=$items item="product"}
{assign var="obj_id" value="`$block.block_id`000`$product.product_id`"}
{if $product}
<li>
{$product.product nofilter}
</li>
{/if}
{/foreach}
</{if $block.properties.item_number == "Y"}ol{else}ul{/if}>
What I need to do is just to replace the following line with the proper code to show the product's default image instead of its name:
{$product.product nofilter}
Any ideas?
Thanks
Try:
{foreach from=$items item="product"}
{if $product}
{assign var="obj_id" value="`$block.block_id`000`$product.product_id`"}
{include file="common/image.tpl" image_width=$block.properties.thumbnail_width image_height=$block.properties.thumbnail_height obj_id=$obj_id images=$product.main_pair href="products.view?product_id=`$product.product_id`"|fn_url}
{/if}
{/foreach}
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}
<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.