tag:help-archives.hannonhill.com,2010-02-09:/discussions/velocity-formats/14350-adding-a-variable-to-itself-in-a-loop-to-produce-a-totalCascade CMS: Discussion 2018-10-18T20:37:58Ztag:help-archives.hannonhill.com,2010-02-09:Comment/401611162016-06-22T16:37:22Z2016-06-22T16:39:09ZAdding a variable to itself in a loop to produce a total<div><p>Stick with the old code. Comment out the code in the macro. Just
output the values of variables, without formatting them (without
the table code). Make sure you see what you want to see first. If
something is missing, try to figure why it is missing. Always use
the following technique when in doubt: with any variable
<code>$x</code>, try <code>$x.class.name</code>. If <code>$x</code>
stores a non-null value, you will see the class name of the object
pointed to by the variable. If it is null, you will see
"$x.class.name" as a string. This will help you debug your
code.</p>
<p>When debugging loops, you can also use built-in variables like
<code>$foreach.index</code>.</p>
<p>Wing</p></div>Wing Ming Chantag:help-archives.hannonhill.com,2010-02-09:Comment/401611162016-06-29T19:11:22Z2016-06-29T19:12:18ZAdding a variable to itself in a loop to produce a total<div><p>Wing,<br>
I couldn't get my Velocity to work using
<code>$_PropertyTool.isNull( $obj )</code> as you suggested, so I
opted for a solution that only checks for the value being empty
which works. I did however notice that if the user enters a "&"
into one of the fields from the data definition it throws an error,
is there something I need to do to escape the output of the values,
that I am not doing to prevent this error?</p>
<pre>
<code>#set ( $page = $_XPathTool.selectSingleNode( $contentRoot, "calling-page/system-page" ) )
#set ( $data = $page.getChild( "system-data-structure" ) )
#set( $progName = $data.getChild( "program-name" ).value )
<p class="program">Course Sequence</p>
#set( $sequence = $data.getChild( "course-sequence" ) )
#set( $studentYears = $sequence.getChildren( "student-year" ) )
#if( $studentYears.size() > 0 )
<div id="accordion">
#foreach( $studentYear in $studentYears )
#set( $year = $_XPathTool.selectSingleNode( $studentYear, "year/value" ).value )
#set( $studTerms = $studentYear.getChildren( "student-term" ) )
<p class="studentYear">$year</p>
<div>
#foreach( $studTerm in $studTerms)
#set( $term = $_XPathTool.selectSingleNode( $studTerm, "term/value" ).value )
<p class="studentTerm">$term</p>
#courseSequence( $year $studTerm )
#end
</div>
#end
</div>
#end
#macro( courseSequence $yr $trm )
#set( $creditTotal = 0 )
<table class="courseSequence">
<tr>
<th>Course</th><th>Credit</th>
</tr>
#foreach( $num in [1..5] )
#set( $noCourse = $trm.getChild( "course-${num}" ).value == '' )
#if( $noCourse )
#break
#end
#set( $courseName = $trm.getChild( "course-${num}" ).value )
#set( $courseCredit = $trm.getChild( "course-${num}-credit" ).value )
#set( $courseCreditInt = $_MathTool.toNumber($courseCredit) )
#set( $creditTotal = $creditTotal + $courseCreditInt)
<tr>
<td>$courseName</td><td>$courseCredit</td>
</tr>
#end
<tr>
<td colspan="2" class="total">Total Credits: <span>$creditTotal</span></td>
</tr>
</table>
#end</code>
</pre></div>voltmer1tag:help-archives.hannonhill.com,2010-02-09:Comment/401611162016-06-29T19:21:06Z2016-06-29T19:22:50ZAdding a variable to itself in a loop to produce a total<div><p>Yes, you need to use the $_EscapeTool object to escape the
character. See <a href="http://help.hannonhill.com/discussions/velocity-formats/10230-escapetool-not-working">
http://help.hannonhill.com/discussions/velocity-formats/10230-escap...</a>
for example.</p>
<p>Wing</p></div>Wing Ming Chantag:help-archives.hannonhill.com,2010-02-09:Comment/401611162016-08-11T12:59:49Z2016-08-11T12:59:49ZAdding a variable to itself in a loop to produce a total<div><p>I've got this working with the $_EscapeTool object added to the
code above. This can be closed<br></p>
<pre>
<code> <td>${_EscapeTool.xml($courseName)}</td><td>$courseCredit</td></code>
</pre></div>voltmer1