Velocity Array Examples

joseph.casabona's Avatar

joseph.casabona

10 Feb, 2014 03:08 PM

Hey Guys,

I can't seem to find any working array examples for Velocity. I basically want to take the breadcrumb code from github (https://github.com/hannonhill/Velocity-Cookbook/tree/master/Breadcr...) and reverse the order for our title pages. My thought was to store what's being printed in an array and reverse the array, but it looks like my syntax is a bit off. I can't really find any good examples of declaring and adding to arrays. Each one I find is different.

Joe

PS- if there is a better way to do this, I'm all ears. I don't know Velocity too well.

  1. 1 Posted by Ryan Griffith on 10 Feb, 2014 05:02 PM

    Ryan Griffith's Avatar

    Hi Joe,

    To confirm, are you looking to simply reverse the order the breadcrumbs are displayed? For example, the current page would be listed first and the Site's homepage would be listed last?

    If so, you can use some recursion along with the Element.getParent() to traverse up the structure, starting with the current page. The following may be a good starting point:

    ## Some constants
    #set ( $defaultPage = "index" )
    #set ( $separator = "»" )
    
    ## Reference the home and current pages
    #set ( $homepage = $_XPathTool.selectSingleNode($contentRoot, "//system-page[path = '/${defaultPage}']") )
    #set ( $page = $_XPathTool.selectSingleNode($contentRoot, "//system-page[@current]") )
    
    #macro (traverseUp $node)
        #set ( $parent = $node.getParent() )
        #if ($parent.getName() == "system-folder")
            <li><a href="${parent.getChild("link").value}/${defaultPage}">${parent.getChild("display-name").value}</a>  ${separator}</li>
            #traverseUp($parent)
        #end
    #end
    
    <ul>
        <li>${page.getChild("display-name").value} ${separator}</li>
        #traverseUp($page)
        <li><a href="/${defaultPage}">${homepage.getChild("display-name").value}</a></li>
    </ul>
    

    Please let me know if you have any questions.

    Thanks!

  2. 2 Posted by joseph.casabona on 10 Feb, 2014 09:44 PM

    joseph.casabona's Avatar

    Ryan,

    Fantastic- that worked. Thanks so much!

    Joe

  3. 3 Posted by Ryan Griffith on 11 Feb, 2014 01:03 PM

    Ryan Griffith's Avatar

    Not a problem at all, Joe. I am glad to hear that format did the trick.

    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!

  4. Ryan Griffith closed this discussion on 11 Feb, 2014 01:03 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