Foreach on list of items
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.
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
1 Posted by Wing Ming Chan on 25 Jun, 2015 05:37 PM
Hi,
To get you started, try the following:
Note that the group is multiple. Therefore, you need to loop through the groups, not the children inside.
Wing
2 Posted by Ryan Griffith on 25 Jun, 2015 06:11 PM
Hi,
Along with Wing's suggested code, I would also recommend checking to see if you have testimonials before outputting the markup:
Please let me know if you have any questions.
Thanks!
3 Posted by iun.nihanson on 25 Jun, 2015 07:37 PM
Thanks Wing and Ryan,
So what is weird, one testimonial will show. But as soon as a entered the foreach statement, it disappears.
4 Posted by Ryan Griffith on 25 Jun, 2015 08:01 PM
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 Posted by iun.nihanson on 25 Jun, 2015 08:33 PM
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 Posted by Ryan Griffith on 26 Jun, 2015 12:16 PM
Hi,
We're definitely getting close. I have a few questions to see if we can narrow things down a bit:
$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 Posted by iun.nihanson on 29 Jun, 2015 06:27 PM
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 Posted by Ryan Griffith on 29 Jun, 2015 06:30 PM
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!
Ryan Griffith closed this discussion on 29 Jun, 2015 06:30 PM.