Forums / Developer / Bug in switch statement?

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

eZ debug

Timing: Jan 30 2025 21:31:51
Script start
Timing: Jan 30 2025 21:31:51
Module start 'content'
Timing: Jan 30 2025 21:31:51
Module end 'content'
Timing: Jan 30 2025 21:31:51
Script end

Main resources:

Total runtime0.2359 sec
Peak memory usage8,192.0000 KB
Database Queries141

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0117 587.9297370.3047
Module start 'content' 0.01170.0147 958.23441,009.6406
Module end 'content' 0.02650.2094 1,967.87503,899.4375
Script end 0.2359  5,867.3125 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00522.2105200.0003
Check MTime0.00160.6801200.0001
Mysql Total
Database connection0.00200.827410.0020
Mysqli_queries0.120751.13871410.0009
Looping result0.00170.71291390.0000
Template Total0.208988.510.2089
Template load0.00140.586610.0014
Template processing0.207587.928110.2075
Override
Cache load0.00090.378910.0009
Sytem overhead
Fetch class attribute can translate value0.00180.782010.0018
XML
Image XML parsing0.00040.175010.0004
General
dbfile0.00964.0728200.0005
String conversion0.00000.003430.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0002 secs