Nested if else in template

60 views
Skip to first unread message

Muhammad Shahzad

unread,
May 30, 2017, 1:01:43 AM5/30/17
to f3-fra...@googlegroups.com
How Can I use nested if else inside the template and assign a value to variable?

 <repeat group="{{ @transactions.subset }}" value="{{ @transaction }}">

<tr>
 
<?php
     
if( isset(@transaction.isCheckApi) &&  @transaction.store.isCheckApi == 1){
       
@setClass = 'orange';
     
}
     
else if( isset(@transaction.isDuplicate) &&  @transaction.isDuplicate == 1){
       
@setClass = 'red';
     
}
       
else if( isset(@transaction.transactionType)){
         
if( @transaction.transactionType == 'sales' )
           $setClass
= 'green';
         
else
            $setClass
= 'blue';
     
}
     
else{
       
@setClass = '';
     
}
   
?>

 
<td class="{{@setClass}}">
      {{@
transactions.name}}
 
</td>
 
<td>
      {{@transactions.commission}}
 
</td>

</tr>

 
</repeat>


ved

unread,
May 30, 2017, 3:45:54 AM5/30/17
to Fat-Free Framework
Use the set tag ?

<set class="{{ foo }}" />





Muhammad Shahzad

unread,
May 30, 2017, 5:09:19 AM5/30/17
to Fat-Free Framework
And what about nested if else? can I do this without starting <?php tags?

ved

unread,
May 30, 2017, 5:44:59 AM5/30/17
to Fat-Free Framework
I don't really understand why you're using php tags in the first place if you're using the Template class.

Nested if/else with the Template class:

<check if="{{ @somecondition }}">
<true>
    first condition is true
</true>
<false>
   
<check if="{{ @anothercondition }}">
       
<true>
            second condition is true
       
</true>
       
<false>
            none of the previous was true, this is the final "else"
       
</false>
   
</check>
</false>
</check>

The equivalent, using the (imho, simpler) Preview class tags

{~ if(@somecondition): ~}
first condition
is true
{~ elseif(@anothercondition): ~}
second condition
is true
{~ else: ~}
none of the previous was
true, this is the final "else"
{~ endif; ~}

xfra35

unread,
May 30, 2017, 6:02:40 AM5/30/17
to Fat-Free Framework
You cannot accomplish "else if" with a simple <check> tag, so you have to nest them as @ved told.

An alternative way is to use a <switch> against TRUE:

<switch expr="TRUE">
 
<case value="{{ isset(@transaction.isCheckApi) &&  @transaction.store.isCheckApi == 1 }}">
   
<set setClass="orange"/>
 
</case>
 
<case value="{{ isset(@transaction.isDuplicate) &&  @transaction.isDuplicate == 1 }}">
   
<set setClass="red"/>
 
</case>
 
<case value="{{ isset(@transaction.transactionType) &&  @transaction.transactionType == 'sales' }}">
   
<set setClass="green"/>
 
</case>
 
<case value="{{ isset(@transaction.transactionType) }}">
   
<set setClass="blue"/>
 
</case>
 
<default>
   
<set setClass=""/>
 
</defaut>
</switch>

Reply all
Reply to author
Forward
0 new messages