path not displaying when display-name is blank

espanae's Avatar

espanae

22 Jan, 2015 05:32 PM

Hi,
I'm working on Cascade 7.8 and can't figure out why the attached velocity script isn't displaying the page path when the display-name's blank (like on lines 45, 71, 97 and 123 of the attached index block)? Here's my if statement :

#if ( $display != "")
<li><a href="${link}">${_EscapeTool.xml($display)}</a></li>
#else
  <li><a href="${link}">${path}</a></li>
#end

The script should list each page's display-name or path if display-name's blank. Thoughts?

Erik

  1. 1 Posted by Ryan Griffith on 22 Jan, 2015 06:50 PM

    Ryan Griffith's Avatar

    Hi Erik,

    The issue is when there is no value for Display Name $page.getChild("display-name") will return null. If you attempt to check either $page.getChild("display-name") or $page.getChild("display-name").value, Velocity will simply dump those statements as plaintext. This will cause your conditional to return true since technically speaking the variable is not an empty string.

    Instead, I suggest one of the following:

    #set ( $display = $page.getChild("display-name") )
        
    ## Print the display-name
    #if ( $display && $display.value != "")
        <li><a href="${link}">${_EscapeTool.xml($display.value)}</a></li>
    #else
        ## Print the path when the display-name is blank.
        <li><a href="${link}">${path}</a></li>
    #end
    

    To be a little more self-documenting, you could use the Property Tool's isNull method to check if the element is null:

    #set ( $display = $page.getChild("display-name") )
        
    ## Print the display-name
    #if ( !$_PropertyTool.isNull($display) && $display.value != "")
        ...
    #else
        ...
    #end
    

    Please let me know if you have any questions.

    Thanks!

  2. 2 Posted by espanae on 22 Jan, 2015 08:31 PM

    espanae's Avatar

    Ryan, thanks for the quick reply. I tried your suggestion, which makes sense, but it still showed display-name instead of path. Here's an excerpt of the HTML now generated:

    <li><a href="...">Judicial Definitions </a></li>                    
    <li><a href="..."></a></li>
    <li><a href="..."></a></li>
    

    Attached is the code I tried. Let me know if you get something different.

  3. 3 Posted by Ryan Griffith on 22 Jan, 2015 09:10 PM

    Ryan Griffith's Avatar

    Thank you for testing and following up, Erik. My apologies the snippet I provided did not appear to work.

    Oddly, using your sample XML as an XML Block and your original Format, I was able to get all but one page to output false.

    I believe part of my issue with testing locally is, depending on your version of Cascade Server, I believe Index Blocks began omitting empty elements as opposed to using a self-closing tag. For example, i am seeing self-closing <display-name/> elements in your sample XML whereas when I was testing locally with an existing Index Block I was not seeing any <display-name> elements.

    A little more complex, but you could possibly try the following:

    #foreach ( $page in $block )
        #set ( $link = $page.getChild("link").value )
        #set ( $display = "" )
        #set ( $path = $page.getChild("path").value )
    
        ## Check to see if the display-name element exists.
        #if ($page.indexOf("display-name") != -1)
            #set ( $display = $page.getChild("display-name").value )
        #end
    
        ## If $display is empty, fall back to the path.
        #if ( $display.trim() == "")
            #set ( $display = $path )
        #end
        
        <li><a href="${link}">${_EscapeTool.xml($display)}</a></li>
    #end
    

    Please let me know if you have any questions.

    Thanks!

  4. 4 Posted by espanae on 23 Jan, 2015 02:49 PM

    espanae's Avatar

    Thanks a bunch, Ryan.
    The more complex code worked. I wouldn't have figured it out without your help.

    -Erik

  5. espanae closed this discussion on 23 Jan, 2015 02:50 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