Bvweijen
Bvweijen

Reputation: 73

In if statement double use $ (smarty)

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}

Upvotes: 0

Views: 155

Answers (2)

IMSoP
IMSoP

Reputation: 97688

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"}

Upvotes: 2

BKM
BKM

Reputation: 7079

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}

Upvotes: 0

Related Questions