switch expr larger, smaller than

36 views
Skip to first unread message

v.

unread,
Oct 11, 2019, 10:17:40 AM10/11/19
to Fat-Free Framework
Hi,
I am trying to get a switch expression in a template to work.
I want the output to state the time of day: midnight, noon, morning,...
It is not working as it should
<switch expr="{{ @i }}"> //i is set in a loop
   
<case value="0" break="{{ TRUE }}"> //ok
        MIDNIGHT
   
</case>
    <case value=" 24 || 48" break="{{ TRUE }}"> //only works for 24, not for 48
        MIDNIGHT
    </case>
    <case value<"12" break="{{ TRUE }}"> //does not work
        MORNING
   
</case>

    <case value="<12" break="{{ TRUE }}"> //also does not work
        MORNING
   
</case>

   
<default>
        OTHER
   
</default>
</switch>
                   

Does the case only work when the value is equal to... or am I missing something?
Message has been deleted

ved

unread,
Oct 11, 2019, 2:20:43 PM10/11/19
to Fat-Free Framework
Hi,

I deleted my previous reply because I didn't even notice those greater than and less than signs. 

AFAIK, you can't do that. I'd recommend something simpler like this:

{~ if(@i == 0 ): ~}
MIDNIGHT
{~ elseif(@i > 12): ~}
AFTERNOON
{~ else: ~}
MORNING
{~ endif; ~}

You can replace that with <check> tags if you'd rather use the Template tags but I find them a bit more hard to read than with the {~ ~}  delimiters.

Cheers

v.

unread,
Oct 11, 2019, 2:33:48 PM10/11/19
to Fat-Free Framework
As always, thanks ved!
I will ook into doing it with the {~~} delimiters

xfra35

unread,
Oct 13, 2019, 3:08:48 AM10/13/19
to Fat-Free Framework
Hi, the <switch> tempate directive follows the same logic as the PHP switch statement, which compares one value with a series of "case" values.
That means comparing $i with "24 || 48" fails because you're comparing an integer with a boolean.

What you can do though, is reverse the logic, and compare TRUE with a series of boolean tests. See:


<switch expr="TRUE">
    
<case value="{{ @i==0 }}">
        MIDNIGHT
    
</case>
    <case value="
{{ @i==24 || @i==48 }}">
        MIDNIGHT
    </case>
    <case value="
{{ @i<12 }}">
        MORNING
    
</case>

    <default>
        OTHER
    
</default>
</switch>

v.

unread,
Oct 14, 2019, 2:59:39 AM10/14/19
to Fat-Free Framework
Thanks xfra35, that's pretty smart!
I have implemented ved's solution in the meantime,
Reply all
Reply to author
Forward
0 new messages