I am using the even and odd logic for that but not getting the output as i need
for example i have array of
$data = array(1000,1001,1002, 1003,1004,1005);
$smarty->assign('data',$data);
{section name=i loop=$data}
{section}
so the output i need is :
<div>
<dl>1000</dl>
<dl>1001</dl>
</div>
<div>
<dl>1002</dl>
<dl>1003</dl>
</div>
<div>
<dl>1004</dl>
<dl>1005</dl>
</div>
According to offical document http://www.smarty.net/docsv2/en/language.function.section.tpl
Try following codes:
<div>
{section name=i loop=$data}
<dl>{$data[i]}</dl>
{if $smarty.section.data.index > 0 && $smarty.section.data.index % 2 == 0 && $smarty.section.data.index < $smarty.section.customer.total -1}
</div><div>
{/if}
{/section}
</div>
You should do it this way:
{section name=i loop=$data}
{if $smarty.section.i.index %2 == 0}
<div>
{/if}
<dl>{$data[i]}</dl>
{if $smarty.section.i.index %2 == 1 || $smarty.section.i.last}
</div>
{/if}
{/section}
<div> should be rather created in section because when there weren't be any items you probably wouldn't like to create empty <div>. Last condition $smarty.section.i.last is added in case you have for example 5 elements and in that case you of course make sure your div is closed.
Related
This code shows me all selections I have made.
How can I filter this and only show the selection of a specific Group?
For example {if $sConfigurator.groupID == 113}
{$configurator = $sArticle.sConfigurator}
{foreach $configurator as $configuratorGroup}
{foreach $configuratorGroup.values as $option}
{if $option.selected}
<div class="selected">
<div class="group">{$configuratorGroup.groupname}</div>
<div class="option">{$option.optionname}</div>
</div>
{/if}
{/foreach}
{/foreach}
I'm also not sure what you mean or what configurator your code is about...
Maybe just add the condition to the if?
{if $option.selected && $configuratorGroup.id == 113}
<div class="selected">
<div class="group">{$configuratorGroup.groupname}</div>
<div class="option">{$option.optionname}</div>
</div>
{/if}
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}
I have a array of images in order:
1-3 are main images.
3+ are thumbnails.
Q) How can I split the thumbs on 2 lines.
E.g:
<div class="main-images">
{section name="i" loop=$images.rows}
{assign var="rows" value=$images.rows[i]}
{if $smarty.section.i.index <= 2}
<img />
{/if}
{/section}
</div>
<div class="thumbs-images">
{math assign=thumbs_count equation="total - other" total=$images.rowcount other=3}
{section name="i" loop=$images.rows}
{assign var="rows" value=$images.rows[i]}
{if $smarty.section.i.index >= 3}
<img />
{/if}
{/section}
</div>
So I need to add something {if $thumbs_count >= $smarty.section.i.index}<div style="clear:both" />{/if} so this appears half way through the second loop.
Managed to figure this out.
{math assign="total_row_split" equation="floor((total - main) / division)" total=$images.rows|#count division=2 main=3}
{counter start=0 print=false assign="thumbs_count"}
{section name="i" loop=$images.rows}
{assign var="rows" value=$images.rows[i]}
{if $smarty.section.i.index >= 3}
{if $thumbs_count == $total_row_split}<br style="clear:both" />{/if}
<a href="{$HOME}/get/image{$rows.filename.fvalue}" rel="fancy" title="{$rows.title.value|default:$product.name.fvalue}">
<img src="{$HOME}/get/image/120{$rows.filename.fvalue}" alt="{$rows.title.value|default:$product.name.fvalue} Picture" />
</a>
{counter print=false}
{/if}
{/section}
I'm in a loop to display products...
4 per row, unlimited rows'
I need to know if it's the nth entry... example every 4 items... So I know its the first column as in
item 1, item5, item 9 etc...
Or last item
item 4, item 8, item 12
Tried these where
{foreach from=$sproducts item="product" name="sproducts"}
{counter assign="bobis" name="bobis" }
{if $bobis is div by 4|| $laster ==1}
{if $bobis mod 4 == 0}
{if $bobis !=4 && $bobis !=8 && $bobis != 12}
Any simple way?
If I understand the question right, just put a col- class on your item:
<div class="col-{$bobis mod 4}">...</div>
You should get the following:
<div class="col-1">...</div>
<div class="col-2">...</div>
<div class="col-3">...</div>
<div class="col-4">...</div>
<div class="col-1">...</div>
<div class="col-2">...</div>
...and so on
If you are using tables this is something I pulled from a script I am currently working on and adapted to your code somewhat. You probably would have to make some changes but it somewhat gives you an idea.
<table>
{foreach from=$sproducts item="product" name="sproducts"}
{if $product#first}<tr>{/if}
<td>{$product}</td>
{if $product#last}</tr>
{else}{if $product#iteration is div by 4}</tr><tr>
{/if}
{/if}
{/foreach}
</table>