Adding a variable to itself in a loop to produce a total
How would I create a variable that would be the total of another variable plus it'self for ever iteration of my foreach loop?
Here is my Velocity format:
#macro( courseSequence $yr $trm )
<table class="courseSequence">
<tr>
<th>Course</th><th>Credit</th>
</tr>
#foreach( $num in [1..5] )
#set( $courseName = $trm.getChild( "course-${num}" ).value )
#set( $courseCredit = $trm.getChild( "course-${num}-credit" ).value )
#set( $creditTotal = $courseCredit += $courseCredit )
<tr>
<td>$courseName</td><td>$courseCredit</td>
</tr>
#end
<tr>
<td> </td><td>Total Credits: $creditTotal</td>
</tr>
</table>
Showing page 2 out of 2. View the first page
Discussions are closed to public comments.
If you need help with Cascade CMS please
start a new discussion.
Keyboard shortcuts
Generic
? | Show this help |
---|---|
ESC | Blurs the current field |
Comment Form
r | Focus the comment reply box |
---|---|
^ + ↩ | Submit the comment |
You can use Command ⌘
instead of Control ^
on Mac
31 Posted by Wing Ming Chan on 22 Jun, 2016 04:37 PM
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
$x
, try$x.class.name
. If$x
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.When debugging loops, you can also use built-in variables like
$foreach.index
.Wing
32 Posted by voltmer1 on 29 Jun, 2016 07:11 PM
Wing,
I couldn't get my Velocity to work using
$_PropertyTool.isNull( $obj )
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?33 Posted by Wing Ming Chan on 29 Jun, 2016 07:21 PM
Yes, you need to use the $_EscapeTool object to escape the character. See http://help.hannonhill.com/discussions/velocity-formats/10230-escapetool-not-working for example.
Wing
34 Posted by voltmer1 on 11 Aug, 2016 12:59 PM
I've got this working with the $_EscapeTool object added to the code above. This can be closed
Tim closed this discussion on 11 Aug, 2016 02:21 PM.