tag:help-archives.hannonhill.com,2010-02-09:/discussions/velocity-formats/12931-fixing-a-nested-loopCascade CMS: Discussion 2015-10-29T12:01:23Ztag:help-archives.hannonhill.com,2010-02-09:Comment/382955142015-10-26T15:45:03Z2015-10-26T15:45:03ZFixing a nested loop<div><p>Hi Matthew,</p>
<p>I am seeing a couple of items you will want to tweak:</p>
<ul>
<li>You are using the incorrect context element in most places, you
want the loop variable not the array</li>
<li>You are accessing <code>goal</code> outside of your outer loop,
so you will most likely only see the first goal</li>
<li>You don't want to use <code>getChild().value</code> to obtain
the array of elements, instead use the XPath Tool</li>
</ul>
<p>When you have a moment, give the following a try and let me know
how it works out:</p>
<pre>
<code>#set($groupings = $_XPathTool.selectSingleNode($contentRoot,"//individual-theme/content/groupings"))
#if ( $groupings.size() > 0 )
<div>
#foreach( $g in $groupings )
#set( $goal = $g.getChild( 'goal' ).value )
#set( $objectives = $_XPathTool.selectNodes($g, 'objective') )
<p>${_EscapeTool.xml($goal)}</p>
#if ( $objectives.size() > 0 )
<ul>
#foreach ($o in $objectives)
<li>${_EscapeTool.xml($o.value)}</li>
#end
</ul>
#end
#end
</div>
#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/382955142015-10-26T15:53:28Z2015-10-26T15:53:28ZFixing a nested loop<div><p>Hey Ryan,</p>
<p>I replaced that section of the format and nothing appears.
Should I attach the whole format and XML?</p></div>matthew.wrentag:help-archives.hannonhill.com,2010-02-09:Comment/382955142015-10-26T16:05:04Z2015-10-26T16:05:04ZFixing a nested loop<div><p>XML<br></p>
<pre>
<code><system-data-structure>
<individual-theme>
<header>
<title>Engaged</title>
<main-image type="file">
<content/>
<path>/pir/strategic-plan/images/Themes/engaged.jpg</path>
<dynamic-metadata>
<name>seo-title</name>
</dynamic-metadata>
<dynamic-metadata>
<name>nav</name>
<value>No</value>
</dynamic-metadata>
<dynamic-metadata>
<name>DepartmentProgram</name>
<value>-- choose --</value>
</dynamic-metadata>
<link>site://www.scranton.edu/pir/strategic-plan/images/Themes/engaged.jpg</link>
<site>www.scranton.edu</site>
<name>engaged.jpg</name>
</main-image>
<quote>“This strategic plan is about everyone at the University - in particular, our outstanding and caring faculty - being committed to serving as proactive supporters, role models, and facilitators in transforming each and every student to passionately pursue and ultimately meet his or her full potential in the spirit of our Jesuit tradition.” Dr. Douglas Boyle, Chairman and Associate Professor, Department of Accounting </quote>
</header>
<content>
<groupings>
<goal>This is a goal</goal>
<objective>This is an objective</objective>
<objective>another objective</objective>
<objective>one more for good measure</objective>
</groupings>
<groupings>
<goal>Another Goal</goal>
<objective>Objective 1</objective>
<objective>Object 2</objective>
<objective>Objective 3</objective>
</groupings>
</content>
</individual-theme>
</system-data-structure></code>
</pre>
<p>Format<br></p>
<pre>
<code>#set($header = $_XPathTool.selectSingleNode($contentRoot,"//individual-theme/header"))
#set($Title = $header.getChild( 'title' ).value)
#set($quote = $header.getChild( 'quote' ).value)
#set($image = $_XPathTool.selectSingleNode($contentRoot,"//individual-theme/header/main-image/link").value)
<header id="home">
<section class="features text-center section-padding" id="features">
<div class="container">
<div class="row col-wrap">
<img class="col-lg-offset-4 header-image" src="$image" alt="#"/>
<div class="col-md-12">
<div>
<h1 class="page-title">$Title</h1>
<h3 class="page-quote">$quote</h3>
</div>
#set($groupings = $_XPathTool.selectSingleNode($contentRoot,"//individual-theme/content/groupings"))
#if ( $groupings.size() > 0 )
<div>
#foreach( $g in $groupings )
#set( $goal = $g.getChild( 'goal' ).value )
#set( $objectives = $_XPathTool.selectNodes($g, 'objective') )
<p>${_EscapeTool.xml($goal)}</p>
#if ( $objectives.size() > 0 )
<ul>
#foreach ($o in $objectives)
<li>${_EscapeTool.xml($o.value)}</li>
#end
</ul>
#end<br> #end
</div>
#end
</div>
<div class="clearfix"></div>
</div>
</div>
</section>
</header></code>
</pre></div>matthew.wrentag:help-archives.hannonhill.com,2010-02-09:Comment/382955142015-10-26T16:06:42Z2015-10-26T16:06:42ZFixing a nested loop<div><p>Hi Matthew,</p>
<p>Another issue I overlooked which is most likely the cause of the
blank output, you need to use <code>selectNodes</code> when
grabbing the <code>groupings</code>, not
<code>selectSingleNode</code>.</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/382955142015-10-26T20:31:29Z2015-10-26T20:31:29ZFixing a nested loop<div><p>That was it, thanks Ryan! I'm trying to achieve an effect within
these lists so is it okay to leave this ticket open for now?</p></div>matthew.wrentag:help-archives.hannonhill.com,2010-02-09:Comment/382955142015-10-26T20:37:21Z2015-10-26T20:37:21ZFixing a nested loop<div><p>Not a problem at all, Matthew. I am glad to hear the adjustments
did the trick.</p>
<blockquote>
<p>I'm trying to achieve an effect within these lists so is it okay
to leave this ticket open for now?</p>
</blockquote>
<p>Sure, feel free to respond to this discussion if you have
additional questions.</p>
<p>Thanks!</p></div>Ryan Griffithtag:help-archives.hannonhill.com,2010-02-09:Comment/382955142015-10-26T20:48:48Z2015-10-26T20:48:48ZFixing a nested loop<div><p>Is there a way to create an ID for each goal and objective list
dynamically?</p>
<p>I'm trying to acheive <a href="http://codepen.io/mattwren88/pen/WQzLQP">this effect</a></p>
<p>I added two snippets to the current code<br></p>
<pre>
<code>#set($groupings = $_XPathTool.selectNodes($contentRoot,"//individual-theme/content/groupings"))
#if ( $groupings.size() > 0 )
<div>
#foreach( $g in $groupings )
#set( $goal = $g.getChild( 'goal' ).value )
#set( $objectives = $_XPathTool.selectNodes($g, 'objective') )
<p><a href="#" onclick="toggler('myContent');"> ${_EscapeTool.xml($goal)} </a></p>
#if ( $objectives.size() > 0 )
<div id="myContent" class='hidden'>
<ul>
#foreach ($o in $objectives)
<li>${_EscapeTool.xml($o.value)}</li>
#end
</ul>
</div>
#end<br> #end
</div>
#end</code>
</pre>
<p>Is there a way I can create an ID and then increment , maybe a
number, by one for each iteration of this?</p></div>matthew.wrentag:help-archives.hannonhill.com,2010-02-09:Comment/382955142015-10-27T11:41:37Z2015-10-27T11:41:37ZFixing a nested loop<div><p>Hi Matthew,</p>
<p>What you can do is use <code>$foreach.index</code> to build your
ID dynamically. Something like the following should do the
trick:</p>
<pre>
<code>#set($groupings = $_XPathTool.selectNodes($contentRoot,"//individual-theme/content/groupings"))
#if ( $groupings.size() > 0 )
<div>
#foreach( $g in $groupings )
#set( $goal = $g.getChild( 'goal' ).value )
#set( $objectives = $_XPathTool.selectNodes($g, 'objective') )
<p><a href="#" onclick="toggler('myContent-${foreach.index}');"> ${_EscapeTool.xml($goal)} </a></p>
#if ( $objectives.size() > 0 )
<div id="myContent-${foreach.index}" class='hidden'>
<ul>
#foreach ($o in $objectives)
<li>${_EscapeTool.xml($o.value)}</li>
#end
</ul>
</div>
#end
#end
</div>
#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/382955142015-10-28T13:02:28Z2015-10-28T13:02:28ZFixing a nested loop<div><p>this worked excellently! Thanks so much!</p></div>matthew.wrentag:help-archives.hannonhill.com,2010-02-09:Comment/382955142015-10-29T12:01:22Z2015-10-29T12:01:22ZFixing a nested loop<div><p>Not a problem at all, Matthew, thank you for following up. I am
glad to hear my proposed snippet did the trick.</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