tag:help-archives.hannonhill.com,2010-02-09:/discussions/velocity-formats/10815-simple-loop-in-velocityCascade CMS: Discussion 2014-09-12T19:55:09Ztag:help-archives.hannonhill.com,2010-02-09:Comment/343750242014-08-28T19:49:20Z2014-08-28T19:49:20ZSimple loop in velocity <div><p>Hi,</p>
<p>I'm just trying to loop through content and display it but it
currently only displays the first input data's values</p>
<p>XML<br></p>
<pre>
<code><system-data-structure>
<title>Documents</title>
<document-links>
<link-title>"Pedro Arrupe, S.J. "Men for Others""</link-title>
<external-link>http://onlineministries.creighton.edu/CollaborativeMinistry/men-for-others.html</external-link>
</document-links>
<document-links>
<link-title>"Peter-Hans Kolvenbach, S.J. "Who Our Students Become"</link-title>
<external-link>http://onlineministries.creighton.edu/CollaborativeMinistry/Kolvenbach/Kolvenbach-SantaClara.pdf</external-link>
</document-links>
<document-links>
<link-title>"Adolfo Nicolas, S.J. "Jesuit Higher Education Today"</link-title>
<external-link>http://www.sjweb.info/documents/ansj/100423_Mexico%20City_Higher%20Education%20Today_ENG.pdf</external-link>
</document-links>
</system-data-structure></code>
</pre>
<p>Velocity<br></p>
<pre>
<code>#set($title = $_XPathTool.selectSingleNode($contentRoot,"//title").value)
#set($linkTitle = $_XPathTool.selectSingleNode($contentRoot,"//link-title").value)
#set($link = $_XPathTool.selectSingleNode($contentRoot,"//external-link").value)<br><br>
<br><div class="body">
<h1>$title</h1>
<div>
<a href="$link"><h2>$linkTitle</h2></a>
</div>
</div></code>
</pre></div>matthew.wrentag:help-archives.hannonhill.com,2010-02-09:Comment/343750242014-08-29T00:37:46Z2014-08-29T00:37:46ZSimple loop in velocity <div><p>Hi Matthew,</p>
<p>Looking over your Velocity, I am not seeing a loop for your
<code><document-links></code> elements. Think of Velocity as
a language like Java, PHP, or JavaScript where you obtain an array
and loop over each element.</p>
<p>When you have a moment, try the following and let me know how it
works out:</p>
<pre>
<code>#set ( $title = $_XPathTool.selectSingleNode($contentRoot, "/system-data-structure/title") )
#set ( $docLinks = $_XPathTool.selectNodes($contentRoot, "//document-links") )
<div class="body">
<h1>${_EscapeTool.xml($title.value)}</h1>
#if ($docLinks.size() > 0)
#foreach ($dl in $docLinks)
#set ( $linkTitle = $_EscapeTool.xml($dl.getChild("link-title").value) )
#set ( $link = $_EscapeTool.xml($dl.getChild("external-link").value) )
<div>
<a href="${link}"><h2>${linkTitle}</h2></a>
</div>
#end
#end
</div></code>
</pre>
<p>Please let me know if you have any questions.</p>
<p>Thanks!</p></div>Ryan Griffith