Adding a variable to itself in a loop to produce a total

voltmer1's Avatar

voltmer1

21 Jun, 2016 02:19 PM

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>&nbsp;</td><td>Total Credits: $creditTotal</td>
    </tr>
    </table>

Showing page 2 out of 2. View the first page

  1. 31 Posted by Wing Ming Chan on 22 Jun, 2016 04:37 PM

    Wing Ming Chan's Avatar

    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

  2. 32 Posted by voltmer1 on 29 Jun, 2016 07:11 PM

    voltmer1's Avatar

    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?

    #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
    
  3. 33 Posted by Wing Ming Chan on 29 Jun, 2016 07:21 PM

    Wing Ming Chan's Avatar

    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

  4. 34 Posted by voltmer1 on 11 Aug, 2016 12:59 PM

    voltmer1's Avatar

    I've got this working with the $_EscapeTool object added to the code above. This can be closed

     <td>${_EscapeTool.xml($courseName)}</td><td>$courseCredit</td>
    
  5. Tim closed this discussion on 11 Aug, 2016 02:21 PM.

Comments are currently closed for this discussion. You can start a new one.

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