Forums / Developer / Change sentinel value inside a switch - possible scope issue

Change sentinel value inside a switch - possible scope issue

Author Message

James Ward

Tuesday 30 September 2003 2:20:59 pm

Hi all.
I would like to create a section that does something different on the first iteration than for all subsequent iterations.

I am attempting to fetch related objects for my example. I placed the following code in an article with 3 related objects:

{let runcheck=0}
{section show=count($node.object.related_contentobject_array)|gt(0)}
{section name=Rel loop=$node.object.related_contentobject_array}
{switch name=FirstRun match=$runcheck}
{case match=0}
First Run runcheck = {$runcheck}<br />
{set $runcheck=1}
{/case}
{case}
Sebsequent Run runcheck = {$runcheck}<br />
{/case}
{/switch}
{/section}
{/section}
{/let}

This returns:
First Run runcheck = 0
First Run runcheck = 0
First Run runcheck = 0

Hardly the desired result. I tried using $#runcheck=1 but it made no difference. I read the documentation on let and set and namespaces. Is it even possible for me to change the value of $runcheck inside my switch or do I need to come at this another way?

Thanks in advance.

working at www.wardnet.com
blogging at www.jamesward.ca

James Ward

Friday 03 October 2003 10:14:57 am

O.K. I'm a little closer to a solution.
Instead of using a sentinel value inside the switch I check the index of the section to see which itteration I'm on. The problem now is how to close my table. Currently the </table> appears regardless of the <table> tag. This can create some dandy layout problems.
Is there a way to check if I am on the last itteration of a section, or a way to set a sentinel variable inside a section so I can tell if its code was run?

Here is the code I am currently using. If someone else has a better way of listing related objects I'd love to hear it.

{*Related Objects List*}
{section name=total show=count($node.object.related_contentobject_array)|gt(0)}
{section name=Rel loop=$node.object.related_contentobject_array}
{section-exclude match=$:item.contentclass_id|eq(5)}
{switch match=$:index}
{case match=0}
<table width="150" border=0 cellpadding=3 cellspacing=0 class="related-information-table">
<tr>
<td class="related-information-title">Related Information</td>
</tr>
<tr>
<td><ul>
<li><a class="related-information-link" href={concat("/content/view/full/",$:item.main_node.node_id)|ezurl}>{$:item.name}</a></li>
{/case}
{case}
<li><a class="related-information-link" href={concat("/content/view/full/",$:item.main_node.node_id)|ezurl}>{$:item.name}</a></li>
{/case}
{/switch}
{/section}
</ul></td>
</tr>
</table>
{/section}
{*End Related Objects List*}

working at www.wardnet.com
blogging at www.jamesward.ca

James Ward

Wednesday 08 October 2003 12:55:38 pm

Here is the working code to create a related object table, allowing you to relate articles without adding any objects to the article itself. Just add the related item and the code will take care of the rest. Use the section exclude to eliminate classes which are related for inclusion in the reference article.

Thanks to Vidar Langseid for the corrected code.

{*Related Objects List*}
{let enableendtag=0 name=Rel}
{section show=count($node.object.related_contentobject_array)|gt(0)}
{section loop=$node.object.related_contentobject_array}
{section-exclude match=$:item.contentclass_id|eq(5)}
{switch match=$:index}
{case match=0}
{* if starttag *}
{set enableendtag=1}
<table width="150" border=0 cellpadding=3 cellspacing=0 class="related-information-table">
<tr>
<td align="center"><img src={"related.gif"|ezimage} width="143" height="16" alt="anemia awareness week" border="0" /></td>
</tr>
<tr>
<td><ul>
<li><a class="related-information-link" href={concat("/content/view/full/",$:item.main_node.node_id)|ezurl}>{$:item.name}</a></li>
{/case}
{case}
<li><a class="related-information-link" href={concat("/content/view/full/",$:item.main_node.node_id)|ezurl}>{$:item.name}</a></li>
{/case}
{/switch}
{/section}
{section show=$:enableendtag}
</ul></td>
</tr>
</table>
{/section}
{/section}
{/let}

working at www.wardnet.com
blogging at www.jamesward.ca