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.
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}
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}
Question about smarty if statement.
I have create a counter and i need the value of the counter in a if statement.
The counter looks like this:
{assign var="counter" value=1}
It looks like this:
{if $task.check_counter == "on"} checked {/if}
It have to looks like:
{if $task.check_1 == "on"} checked {/if}
Is this possible?
UPDATE:
Part of the code:
{assign var="counter" value=1}
{foreach from=$service item=sert}
{if $sert.parent_id == $task.task_id}
<tr>
<td>{$sert.name}</td>
<td>{$address_detail.start}</td>
<td style="text-align: center;">
<div>
<input type="checkbox"
class="icheck" name="c_{$counter}"
id="icheck_{$counter}"
{if $task.check_$counter == "on"} checked {/if} >
</div>
</td>
</tr>
{assign var="counter" value=$counter + 1}
{/if}
{/foreach}
99 times out of 100 where you are using "variable variables", you should actually be defining an array.
If instead of dynamically naming your items as check_1, check_2, etc, you defined an array of items called check_list, you could just do this: {$task.check_list.$counter}
Note that you can even do this with form fields, as PHP will turn fields like <input name="task[check][1]" /> into appropriate arrays when it processes the form submission.
That said, since this particular case isn't actually a variable name but an array key within the $task array, you can define the whole key as a dynamic string, and look up using that:
{assign var=array_key value='check_'|cat:$counter}
{if $task.$array_key == "on"}
You could try this;
Assign your smarty variable like;
$smarty->assign('counter', 1);
Then try this
{if $task.check_$smarty->get_template_vars('counter') == "on"} checked {/if}
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.