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.

eZ debug

Timing: Jan 19 2025 06:29:07
Script start
Timing: Jan 19 2025 06:29:07
Module start 'layout'
Timing: Jan 19 2025 06:29:07
Module start 'content'
Timing: Jan 19 2025 06:29:09
Module end 'content'
Timing: Jan 19 2025 06:29:09
Script end

Main resources:

Total runtime1.6615 sec
Peak memory usage4,096.0000 KB
Database Queries68

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0055 590.2656152.6250
Module start 'layout' 0.00550.0060 742.890639.4453
Module start 'content' 0.01161.6474 782.3359624.0859
Module end 'content' 1.65900.0024 1,406.421916.7891
Script end 1.6614  1,423.2109 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00330.2011160.0002
Check MTime0.00130.0787160.0001
Mysql Total
Database connection0.00080.045610.0008
Mysqli_queries1.589295.6481680.0234
Looping result0.00080.0510660.0000
Template Total1.605096.620.8025
Template load0.00230.136320.0011
Template processing1.602796.457120.8013
Template load and register function0.00060.033310.0006
states
state_id_array0.00170.101710.0017
state_identifier_array0.00360.216720.0018
Override
Cache load0.00190.1160450.0000
Sytem overhead
Fetch class attribute can translate value0.00140.081730.0005
Fetch class attribute name0.00150.088080.0002
XML
Image XML parsing0.00120.070430.0004
class_abstraction
Instantiating content class attribute0.00000.001390.0000
General
dbfile0.00120.0737220.0001
String conversion0.00000.000740.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
6content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
14content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
5content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
3content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
3content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 33
 Number of unique templates used: 7

Time used to render debug report: 0.0002 secs