Dealing with empty node

steelej's Avatar

steelej

18 Oct, 2012 04:22 PM

I'm encountered problems similar to this before, but I haven't found my way through this one yet. I'd be much obliged for some assistance.

These are co-op profile pages. There is node for entering the employer's URL. When one is entered I want to format the employer's name linked to the site. When no URL is entered I want to show just the employer's name.

  1. 1 Posted by Ryan Griffith on 18 Oct, 2012 05:41 PM

    Ryan Griffith's Avatar

    Hi,

    I believe the issue you may be running into is that you are using Xpath within the getChild() method, which I don't think is valid.

    What I would do is change

    #set ($url = $page.getChild("system-data-structure").getChild("content").getChild("url[. != '']").value)

    to

    #set ($url = $_EscapeTool.url($page.getChild("system-data-structure").getChild("content").getChild("url).value))

    This will grab the value entered and escape the string to ensure it's a valid URL.

    Then at the bottom when you are checking for the existence of a URL, use something like the following:

    #if ($url.trim() != "")
        <a href="${url}">${employer}</a>
    #else
        ${employer}
    #end
    

    Please let me know if that does the trick or if you have any questions.

    Thanks

  2. 2 Posted by steelej on 18 Oct, 2012 05:51 PM

    steelej's Avatar

    Thanks. that did the trick. Can you give me a quick and dirty explanation of the .trim function?

  3. 3 Posted by Ryan Griffith on 18 Oct, 2012 06:01 PM

    Ryan Griffith's Avatar

    Glad to hear that did the trick.

    Can you give me a quick and dirty explanation of the .trim function?

    The String.trim() method basically removes any leading and trailing whitespace from the string. It's not required; however, if the user enters a space and nothing else, the string would technically not be blank so you would be outputting a broken link. The trim would take care of that for you.

  4. steelej closed this discussion on 25 Oct, 2012 01:19 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