Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Matt Bradley 6 posts 27 karma points
    Apr 13, 2010 @ 17:13
    Matt Bradley
    0

    Conditional section in a template

    Hi all,

    <% if(umbraco.presentation.nodeFactory.Node.GetCurrent().GetProperty("aliasName").ToString()!=""){ %>

    Why does this ALWAYS evaluate to true, even if there's no data in the variable aliasName?

    How can I check for th presence / state of a document value in templates?

     

     

     


     

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Apr 13, 2010 @ 17:23
    Hendy Racher
    0

    Hi Matt,

    To get the Value of a property you need :

    umbraco.presentation.nodeFactory.Node.GetCurrent().GetProperty("aliasName").Value.ToString()

    (bear in mind the Value can be a null, in which case the ToString() will error - I use some helper methods to prevent having to do these null checks each time)

    HTH,

    Hendy

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Apr 13, 2010 @ 17:45
    Hendy Racher
    1

    eg.

    <%@ Import namespace="umbraco.presentation.nodeFactory" %>
    <script runat="server">
        public static string GetNodeProperty(string propertyName)
        {
            return GetNodeProperty(Node.GetCurrent(), propertyName);
        }

        public static string GetNodeProperty(int nodeId, string propertyName)
        {
            return GetNodeProperty(new Node(nodeId), propertyName);
        }

        public static string GetNodeProperty(Node node, string propertyName)
        {
            string propertyValue = string.Empty;

            if (node != null && node.GetProperty(propertyName) != null)
            {
                propertyValue = node.GetProperty(propertyName).Value;
            }

            return propertyValue;
        }
    </script>
    <% if (GetNodeProperty("aliasName") != string.Empty) {%>

    ...

    <%}%>
  • Matt Bradley 6 posts 27 karma points
    Apr 13, 2010 @ 21:15
    Matt Bradley
    0

    Thanks! Works perfectly. :D

     

Please Sign in or register to post replies

Write your reply to:

Draft