Foreach on list of items

iun.nihanson's Avatar

iun.nihanson

25 Jun, 2015 03:25 PM

I'm trying to create a testimonial slider. How it works is each testimonial will display in a slider with an option for a popup screen to show all. I know I need some kind of a foreach loop. I can't seem to figure it out. I'm recently new to velocity.

  1. 1 Posted by Wing Ming Chan on 25 Jun, 2015 05:37 PM

    Wing Ming Chan's Avatar

    Hi,

    To get you started, try the following:

    #set( $testimonials = $_XPathTool.selectNodes( $contentRoot, "/system-data-structure/testimonials" ) )
    #rendertestimonials( $testimonials )
    #macro( rendertestimonials $testimonials )
    <!--slider that shows testimonials-->
    <div class="wrapper container">
    <ul class="testimonials">
      #foreach( $testimonial in $testimonials )
        #set( $name         = $testimonial.getChild( 'name' ).value )
        #set( $organization = $testimonial.getChild( 'organization' ).value )
        #set( $content      = $testimonial.getChild( 'content' ).value )
        #set( $timg         = $testimonial.getChild( 'image' ).getChild( 'path' ).value )
        <li>
          ## your code here
        </li>
      #end
    </ul>
    </div>
    #end
    

    Note that the group is multiple. Therefore, you need to loop through the groups, not the children inside.

    Wing

  2. 2 Posted by Ryan Griffith on 25 Jun, 2015 06:11 PM

    Ryan Griffith's Avatar

    Hi,

    Along with Wing's suggested code, I would also recommend checking to see if you have testimonials before outputting the markup:

    #if( $testimonials.size() > 0 )
      #rendertestimonials( $testimonials )
    #end
    

    Please let me know if you have any questions.

    Thanks!

  3. 3 Posted by iun.nihanson on 25 Jun, 2015 07:37 PM

    iun.nihanson's Avatar

    Thanks Wing and Ryan,
    So what is weird, one testimonial will show. But as soon as a entered the foreach statement, it disappears.

  4. 4 Posted by Ryan Griffith on 25 Jun, 2015 08:01 PM

    Ryan Griffith's Avatar

    Hi,

    I didn't have a chance to test this, but it appears you are ending your #foreach too soon. Try moving the #end lower down in your macro to the appropriate place.

    Please let me know if you have any questions.

    Thanks!

  5. 5 Posted by iun.nihanson on 25 Jun, 2015 08:33 PM

    iun.nihanson's Avatar

    Hi Ryan,
    So this is the correct code. The one above i had accidentally deleted somethings out of it. So I did move the #end after the </li> still nothing.
    So removed the bottom code and right now I'm just focusing on the slider.

    #set( $testimonials = $_XPathTool.selectNodes( $contentRoot, "/system-data-structure/testimonials" ) )
    #rendertestimonials( $testimonials )
    #macro(rendertestimonials $testimonials)
        <div class="testimonials-wrapper container">
            <ul class="testimonials">
                 #foreach($testimonial in $testimonials)
                    #set($content = $testimonial.getChild('content').value)
                    #set($timg = $testimonial.getChild('image').getchild('path').value)
                    #set($name = $testimonial.getChild('name').value)
                    #set($organization = $testimonial.getChild('organization').value)
                         <li>
                            <p>$content</p>
                               <div class="author">
                                 <img src="timg" />
                                    <ul class="author-info">
                                         <li>$name</li>
                         <li>$organization</li>
                     </ul>
                              </div>
                        </li>
                    #end
         </ul>
         <a href="#0" class="see-all">See all</a>
        </div>
    #end

  6. 6 Posted by Ryan Griffith on 26 Jun, 2015 12:16 PM

    Ryan Griffith's Avatar

    Hi,

    We're definitely getting close. I have a few questions to see if we can narrow things down a bit:

    • Can you confirm if your XPath is correct when obtaining the testimonials? That is, is the Format applied to the page's DEFAULT region without an Index Block, or to a region with an Index Block?
    • Can you try outputting $testimonials.size() before calling the macro and at the top of the macro to see what values you get. We should expect to see more than one assuming there are more than one entered on the page.

    Please let me know if you have any questions.

    Thanks!

  7. 7 Posted by iun.nihanson on 29 Jun, 2015 06:27 PM

    iun.nihanson's Avatar

    Hi Ryan,
    I figured out my problem. I just restructured my Data Def. and add another group/wrapper. Then in my format in my for each statement after testimonials I added getChildren. My code if now functioning. Thank you so much for your help and assistance!

  8. 8 Posted by Ryan Griffith on 29 Jun, 2015 06:30 PM

    Ryan Griffith's Avatar

    Thank you for following up, I am glad to hear you were able to work through the issue and get your Format working.

    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.

    Have a great day!

  9. Ryan Griffith closed this discussion on 29 Jun, 2015 06:30 PM.

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

 

26 Aug, 2016 01:19 PM
25 Aug, 2016 03:02 PM
25 Aug, 2016 12:50 PM
24 Aug, 2016 08:43 PM
24 Aug, 2016 07:20 PM
21 Aug, 2016 01:20 PM