tag:help-archives.hannonhill.com,2010-02-09:/discussions/velocity-formats/9597-help-w-for-eachCascade CMS: Discussion 2014-04-04T19:13:01Ztag:help-archives.hannonhill.com,2010-02-09:Comment/324141712014-04-04T18:35:55Z2014-04-04T18:35:55ZHelp w/ For Each<div><p>Hi Graham,</p>
<p>There are two ways you could go about doing this:</p>
<ul>
<li>Use the XPath Tool to filter your elements by the field value.
This way, you are only looping over the items that you want</li>
<li>Add an <code>#if</code> statement to check the value of your
field, and if the condition is true do some code or output
something</li>
</ul>
<p>Please let me know if you have any questions.</p>
<p>Thanks!</p></div>Ryan Griffithtag:help-archives.hannonhill.com,2010-02-09:Comment/324141712014-04-04T18:37:23Z2014-04-04T18:37:23ZHelp w/ For Each<div><p>Thanks! I was thinking the #if - how would you do the tool
filter for a field?</p>
<p>Graham Lewis<br>
Webmaster<br>
Jacksonville State University</p>
<p>256.782.5331<br>
<a href="http://www.jsu.edu">www.jsu.edu</a></p></div>Graham Lewistag:help-archives.hannonhill.com,2010-02-09:Comment/324141712014-04-04T18:43:22Z2014-04-04T18:43:22ZHelp w/ For Each<div><p>Not a problem at all. You would use the
<code>XPathTool.selectNodes()</code> function in conjunction with
an XPath expression that checks the value of the field you
want.</p>
<p>If you could attach some sample XML from the Index Block and
what field and value you want to filter by, I would be happy to
help out with the XPath expression.</p>
<p>Please let me know if you have any questions.</p>
<p>Thanks!</p></div>Ryan Griffithtag:help-archives.hannonhill.com,2010-02-09:Comment/324141712014-04-04T18:45:49Z2014-04-04T18:50:09ZHelp w/ For Each<div><p>Sure - here it is.</p>
<pre>
<code><system-block id="2db3b9797f000001036762711cbc28f1">
<name>Academic and Student Affairs</name>
<path>/listings/Academic and Student Affairs</path>
<site>_Sitemap</site>
<system-data-structure definition-path="Listing Builder">
<name>Academic &amp; Student Affairs</name>
<letter>a</letter>
<category>
<value>Academic</value>
<value>Student</value>
</category>
<link-chooser />
<page type="page">
<content />
<path>/index</path>
<dynamic-metadata>
<name>navigation</name>
<value>No</value>
</dynamic-metadata>
<link>site://Academic Affairs/index</link>
<site>Academic Affairs</site>
<name>index</name>
<display-name>Academic Affairs</display-name>
<title>Welcome</title>
</page>
<url />
</system-data-structure></code>
</pre>
<p>Graham Lewis<br>
Webmaster<br>
Jacksonville State University</p>
<p>256.782.5331<br>
<a href="http://www.jsu.edu">www.jsu.edu</a></p></div>Graham Lewistag:help-archives.hannonhill.com,2010-02-09:Comment/324141712014-04-04T18:54:07Z2014-04-04T18:54:07ZHelp w/ For Each<div><p>Thank you for providing the XML, Graham. So let's say you want
to filter <strong>system-blocks</strong> by the letter
<strong>a</strong>:</p>
<pre>
<code>#set ($blocks = $_XPathTool.selectNodes($contentRoot, "//system-block[system-data-structure/letter = 'a']"))
#if ($blocks.size() > 0)
... Do something with blocks ...
#end</code>
</pre>
<p>Or, let's say you want the letter to be dynamic based on a field
on your page called <strong>letter</strong>:</p>
<pre>
<code>#set ($letter = $_XPathTool.selectSingleNode($contentRoot, "//calling-page/system-page/system-data-structure/letter"))
#set ($blocks = $_XPathTool.selectNodes($contentRoot, "//system-block[system-data-structure/letter = '${letter}']"))
#if ($blocks.size() > 0)
... Do something with blocks ...
#end</code>
</pre>
<p>Please let me know if you have any questions.</p>
<p>Thanks!</p></div>Ryan Griffithtag:help-archives.hannonhill.com,2010-02-09:Comment/324141712014-04-04T18:57:31Z2014-04-04T18:57:31ZHelp w/ For Each<div><p>Ah - that makes sense. That's gonna be handy! Thanks</p>
<p>Graham Lewis<br>
Webmaster<br>
Jacksonville State University</p>
<p>256.782.5331<br>
<a href="http://www.jsu.edu">www.jsu.edu</a></p></div>Graham Lewistag:help-archives.hannonhill.com,2010-02-09:Comment/324141712014-04-04T19:13:00Z2014-04-04T19:13:00ZHelp w/ For Each<div><p>Not a problem at all, Graham. I am glad to hear the snippets
helped out.</p>
<p>I'm going to go ahead and close this discussion, please feel
free to comment or reply to re-open if you have any additional
questions.</p>
<p>Have a great day!</p></div>Ryan Griffith