Formating a Block Index

Fernando's Avatar

Fernando

19 Apr, 2016 01:37 PM

I am learning how to use velocity and move most of the stuff I have in velocity i am working to move to XSLT. So I am learning in more detail how to use velocity, I have a block file I am wanting to call in my format that I do not know how to call. Could I get some assistance with this?

Thanks!

  1. 1 Posted by Ryan Griffith on 19 Apr, 2016 04:46 PM

    Ryan Griffith's Avatar

    Hi Fernando,

    A couple of things that jump out at me:

    • You are attempting to grab the block's content using the $page variable as the context. This will require adding system-data-structure to your XPath:
    • Based on the XML you provided, your XPath to obtain the links is incorrect:
      #set($links = $_XPathTool.selectNodes($page, "system-data-structure/related/links/links-list/content/system-data-structure/link"))
      
    • If you need to access the actual URL for the symlink, you will need to adjust your XPath a bit more:
      #set($links = $_XPathTool.selectNodes($page, "system-data-structure/related/links/links-list/content/system-data-structure/link/content/system-symlink"))
      
    • Check to make sure $links.size() is greater than 0 prior to looping to avoid Null Pointer errors

    Please let me know if you have any questions.

    Thanks!

  2. 2 Posted by Fernando on 20 Apr, 2016 04:31 PM

    Fernando's Avatar

    Should I add the system-data-structure to the $page variable? or would it be fine in the $links variable?

    How would I check if the link size is greater than 0 prior to looping?

    Thanks for all of the help!

  3. 3 Posted by Ryan Griffith on 20 Apr, 2016 06:29 PM

    Ryan Griffith's Avatar

    Hi Fernando,

    Should I add the system-data-structure to the $page variable? or would it be fine in the $links variable?

    I added "system-data-structure" to the XPath used to obtain the links so it should not be necessary to add it anywhere else.

    How would I check if the link size is greater than 0 prior to looping?

    You would use $links.size() in an #if statement:

    #if ($links.size() > 0)
        ## Place outputting of links here.
    #end
    

    Please let me know if you have any questions.

    Thanks!

  4. 4 Posted by Fernando on 20 Apr, 2016 07:41 PM

    Fernando's Avatar

    So how would call the outputting of links? on the line where I have <li></li>? I tried doing "<li>$_SerializerTool.serialize($links, true)</li>" but it didnt work. It was not rendering the right information.

  5. 5 Posted by Ryan Griffith on 20 Apr, 2016 08:13 PM

    Ryan Griffith's Avatar

    Hi Fernando,

    This would depend on which XPath you chose to use. Also, you will be doing this within a #foreach loop.

    #set($links = $_XPathTool.selectNodes($page, "system-data-structure/related/links/links-list/content/system-data-structure/link"))
    

    In this case, you can use $link.getChild("content").getChild("system-symlink").value OR $_XPathTool.selectSingleNode($link, "content/system-symlink").value.

    #set($links = $_XPathTool.selectNodes($page, "system-data-structure/related/links/links-list/content/system-data-structure/link/content/system-symlink"))
    

    In this case, you would simply use $link.value.

    Please let me know if you have any questions.

    Thanks!

  6. 6 Posted by Fernando on 22 Apr, 2016 02:15 PM

    Fernando's Avatar

    I am getting close but I do not think i am quite there. This is what I am currently working on.

    <ul>
        #foreach ( $link in $links )
           #if ($links.size() > 0)
              #set($links = $_XPathTool.selectNodes($page, "system-data-structure/related/links/links-list/content/system-data-structure/link"))
            #end
               <li>$link.getChild("content").getChild("system-symlink").value</li>
           #end
    </ul>

    It is showing me blank whenever I see the result of that. Am I missing something ?

    Thanks for all of the help

  7. 7 Posted by Ryan Griffith on 22 Apr, 2016 02:59 PM

    Ryan Griffith's Avatar

    Hi Fernando,

    You are definitely getting close, but need to restructure this code a bit. When you have a moment, give the following a try and let me know how it works out:

    #set($links = $_XPathTool.selectNodes($page, "system-data-structure/related/links/links-list/content/system-data-structure/link")) 
    #if ($links.size() > 0) 
    <ul> 
        #foreach ( $link in $links ) 
               <li>$link.getChild("content").getChild("system-symlink").value</li> 
        #end 
    </ul>
    #end
    

    Please let me know if you have any questions.

    Thanks!

  8. 8 Posted by Fernando on 22 Apr, 2016 03:29 PM

    Fernando's Avatar

    I got this error whenever I put that code in, not sure what happened since the Error line is 41 and my script is only 38 lines long.

    Thanks again for the help!

  9. 9 Posted by Ryan Griffith on 22 Apr, 2016 07:45 PM

    Ryan Griffith's Avatar

    Hi Fernando,

    It sounds like the XML validation during rendering does not like the parameters in those links; the ampersands specifically. When you have a moment, try adjusting the following:

    <li>$link.getChild("content").getChild("system-symlink").value</li>
    

    to:

    <li>${_EscapeTool.xml($link.getChild("content").getChild("system-symlink").value)}</li>
    

    This should properly escape the ampersands.

    Please let me know if you have any questions.

    Thanks!

  10. 10 Posted by Fernando on 25 Apr, 2016 01:40 PM

    Fernando's Avatar

    Got it to work! At first it was only rendering the links so I did some changes.

    And it works!! Thanks for all of the help!

  11. Fernando closed this discussion on 25 Apr, 2016 01:40 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