Velocity - value in foreach loop being reused



I'm using velocity with Cascade Server and I have to loop through some meta-data



<system-index-block name="foo" type="folder" current-time="1421260005397">
<system-page id="e538f3467f0001011e5362ee95c3a8ca">
<name>name</name>
<dynamic-metadata>
<name>image</name>
<!-- no <value> tag -->
</dynamic-metadata>
</system-page>
<system-page id="e538f3467f0001011e5362ee95c3a8ca">
<name>name</name>
<dynamic-metadata>
<name>image</name>
<value>path/to/image</value>
</dynamic-metadata>
</system-page>
<system-page id="e538f3467f0001011e5362ee95c3a8ca">
<name>name</name>
<dynamic-metadata>
<name>image</name>
<!-- no <value> tag -->
</dynamic-metadata>
</system-page>
</system-index-block>


So In this case I have to loop through each <system-page> tag and check the <dynamic-metadata> tag. If the page has an image path, it'll be inside a <value> tag like on the second element.


Heres my wee loop:



#foreach ( $section in $articles)
#set ( $image = $section.getChild("dynamic-metadata").getChild("value").value )

<section>

#if ($image)
<h1>Do this</h1>
#else
<h3>Do this instead</h3>
#end

</section>

#end


The expected output here should be :



<section>
<h3>Do this instead</h3>
<section>
<section>
<h1>Do this</h1>
<section>
<section>
<h3>Do this instead</h3>
<section>


However what I get is:



<section>
<h3>Do this instead</h3>
<section>
<section>
<h1>Do this</h1>
<section>
<section>
<h1>Do this</h1>
<section>


So there you have it. When a set in the iteration has a value for the path, and the following set does not, the value from the previous set is carried over.


Why?


I hope I was able to explain this in a somewhat reasonable matter.


No comments:

Post a Comment