Forums / Developer / Setting the value of variables in a switch statement

Setting the value of variables in a switch statement

Author Message

joe hobbs

Monday 04 July 2005 3:29:17 am

Hello
I am using a drop down menu to allow a visitor to search for facilities in a region. Much of the solution to that was kindly provided by members of the eZ community on these boards. Now I want to modify the feature further. To achieve that I using a 'switch' statement which is in a html form. It seems simple enough, but I am not having any success. I suspect it is something to do with using a switch to set the value of a variable. I include the code and I wonder if any one could point me in the right direction? Incidently, if the switch statement were successful I would pass the value by method = 'get' to a template file to be used as an argument in a fetch.
As usual many thanks for your good attention.

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

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}