Share » Forums » Developer » Setting the value of variables in a...

Setting the value of variables in a switch statement

Setting the value of variables in a switch statement

Monday 04 July 2005 3:29:17 am - 5 replies

Author Message

Eivind Marienborg

Monday 04 July 2005 5:28:02 am

You have to use set-tags, like this:

{*I want to set the value of app depending on the page_ID*}
{let pageid=$node.node_id
            app=' '}
{switch name=pagetester match=$pageid}
{case match=72}
{set $app='petrol'}
{/case}
{case match=71}
{set $app='pubs'}
{/case}
{case}
default case
{/case}
{/switch}
{/let}

Also, it's a good thing to make it a habit to close your let's :)

joe hobbs

Monday 04 July 2005 6:35:59 am

Thank you Eivind
I edited the code with your suggestion (below). Unfortunately the value of the variable 'app' does not get reset to 'edu' in the switch and retains the initial value, 'test' when I output it. Logic tells me this should work, but what is missing?

{let pageid=$node.node_id app='test'}
{switch name=pagetester match=$pageid}
{case match=73}
        {set $app='edu'}
{/case}
{case}
        default
{/case}
{/switch}
{/let}

Eivind Marienborg

Monday 04 July 2005 6:45:50 am

Hmm, sorry, I can't seem to find any errors in your code there.. Anyone else?

Trevor Clowry

Wednesday 06 July 2005 8:05:18 am

I've had the same problem as this on several occasions and can't find the solution.
In a similar example to Joe Hobbs version I have tested the node_id before the switch statement. When the node id is equal to 73 it enters the case:

{case match=73}
        {set $app='edu'}
{/case}

However when i test the $app variable after the switch statement it is still set to its original value. It seems like the $app variable is not being set.
Can somebody try joe's code in a template file and see if it works?
Can anyone suggest a workaround to this?
Thanks again.
Trevor

joe hobbs

Friday 08 July 2005 8:27:39 am

To conclude on this post. I used an if statement to resolve the problem I experienced with the switch (see code below). If anyone knows why the switch would not set the value of the variable whereas the if statement will I would be most interested to learn.
Many thanks

{let pageid=$node.node_id app=''}
{if eq( $pageid, 73 )}
    {set $app='edu'}
{else}
    {set $app='default'}
{/if}


You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu