Bug in switch statement?

Author Message

Trygve Fridstrøm

Monday 30 January 2006 9:04:03 am

The switch statement in the following code doesen't seem to be able to read the declared variables:

{def $ListCourseTypesID=array()}
{def $ListCourses=array()}
{ ... building arrays ...}
{if gt($ListCourseTypesID|count(),0)}
        {foreach $ListCourses as $course}
            {set CourseTypeID= ... something ...}
            --> {$ListCourseTypesID.0} and {$CourseTypeID} are equal here <--
            {switch match=$CourseTypeID}
                {case in=$ListCourseTypesID}
                    -->Never enters here <--
                    {node_view_gui view=listitem content_node=$course}
                {/case}
            {/switch}
        {/foreach}
{/if}

However, if I change the surrounding if to a section statement, it works

{section show=$ListCourseTypesID}
        {foreach $ListCourses as $course}
            {set CourseTypeID= ... something ...}
            --> {$ListCourseTypesID.0} and {$CourseTypeID} are equal here <--
            {switch match=$CourseTypeID}
                {case in=$ListCourseTypesID}
                    -->Enters here, as expected <--
                    {node_view_gui view=listitem content_node=$course}
                {/case}
            {/switch}
        {/foreach}
{/section}

I also tried to use a simpler case statement:

{case in=$ListCourseTypesID.0}

which didn't make any difference. Tried also to use $: in the variable references. That didn't help either. This seems to be a bug? The documentation on the switch statement doesen't mention any dependency between switch and section. Looks like switch operates in a different namespace, although I haven't specified any.

I'm running ez version 3.6.2 on Linux.

André R.

Monday 13 February 2006 8:33:01 am

tried changing the switch statment to if statment ??

...
           {set CourseTypeID= ... something ...} 
           --> {$ListCourseTypesID.0} and {$CourseTypeID} are equal here <-- 
           {if eq($CourseTypeID , $ListCourseTypesID)}
                   -->Never enters here <-- 
                   {node_view_gui view=listitem content_node=$course} 
           {/if} 
....

Either way you don't need a switch when there is only one case.

EDIT: section actually loops, see documentation:
http://ez.no/products/ez_publish_open_source_enterprise_cms/documentation/reference/template_functions/program_flow/section

so you'll have to loop thru the $ListCourseTypesID array (a loop in a loop..)

eZ Online Editor 5: http://projects.ez.no/ezoe || eZJSCore (Ajax): http://projects.ez.no/ezjscore || eZ Publish EE http://ez.no/eZPublish/eZ-Publish-Enterprise-Subscription
@: http://twitter.com/andrerom

Trygve Fridstrøm

Tuesday 14 February 2006 1:16:52 am

Thanks for your reply.

The reason I used switch was that it's easy to check if the match variable value exists in an array (see documentation of switch statement):
{switch match=$CourseTypeID}
{case in=$ListCourseTypesID}

I can't use an if-statement to compare $CourseTypeID and $ListCourseTypesID since one is an array and the other is a single value.

Section doesen't loop unless one uses loop=... in the section definition.

Trygve Fridstrøm

Tuesday 14 February 2006 1:23:07 am

Besides, I would like to avoid using the section statement, since it's not so easy to read and also deprecated according to the documentation.

André R.

Wednesday 15 February 2006 3:14:57 pm

Ok,

but you'll have to change
{if gt($ListCourseTypesID|count(),0)}
to a loop as this line
{section show=$ListCourseTypesID}
makes the code loop thru $ListCourseTypesID

either a new 'foreach' or 'for'

eZ Online Editor 5: http://projects.ez.no/ezoe || eZJSCore (Ajax): http://projects.ez.no/ezjscore || eZ Publish EE http://ez.no/eZPublish/eZ-Publish-Enterprise-Subscription
@: http://twitter.com/andrerom

Hans Melis

Wednesday 15 February 2006 11:40:25 pm

André,

A section does not loop unless the "loop" parameter has been specified. Only using the "show" parameter makes it a conditional statement. The old documentation is not always right. Anyways, section has been deprecated as of 3.6 and the new control/looping structures should be used.

Trygve,

You can use an if statement instead of a switch to compare the string and the array. Your switch construct checks if a $CourseTypeID is in the $ListCourseTypesID array. This can be done in a more elegant way with an if statement:

{if $ListCourseTypesID|contains( $CourseTypeID )}

Why the switch doesn't work when inside an if-block is not clear to me without trying it. But try the if I mentioned to see if that alleviates the problem.

Hans
http://blog.hansmelis.be

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.