Copied to clipboard

Flag this post as spam?

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


  • mmmoustache 50 posts 79 karma points
    Jan 12, 2012 @ 12:28
    mmmoustache
    0

    Check if page has two properties

    Is it possible to check if a page has two properties, and if they both have values then *do something*? I have two dynamic lists for two separate content pickers, with a div wrapping them both. But I only want to show the div if at least one of the properties has a value.

    At the moment can get it to show the div if one of the properties has a value, but can I check for two properties in the same if statement? My code so far:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
       if(@Model.HasProperty("propertyOne"&@Model.GetProperty("propertyOne").Value != String.Empty){
          <div class="wrapper"></div>
        }
    } 


    My goal:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
       if(@Model.HasProperty("propertyOne" *AND/OR* "propertyTwo"&@Model.GetProperty("propertyOne" *AND/OR* "propertyTwo" ).Value != String.Empty){
         <div class="wrapper"></div>
        }
    }
  • alimac 182 posts 371 karma points
    Jan 12, 2012 @ 12:50
    alimac
    0

    I realise this may not be the cleanest solution but you could do something like this:

    bool exists = false;
    if(@Model.HasProperty("propertyOne") && @Model.GetProperty("propertyOne").Value != string.Empty)
    {
    exists = true;

    if(@Model.HasProperty("propertyTwo") && @Model.GetProperty("propertyTwo").Value != string.Empty)
    {
    exists = true;


    if(exists == true)
    {
    <div class="wrapper"></div>

    I think this may work? There's probably a better solution though...

  • Owen 123 posts 246 karma points
    Jan 12, 2012 @ 12:57
    Owen
    0

    You can write a extension method for this purpose, something like:

     public static class DynamicNodeEx
        {
            public static bool HasProperties(this DynamicNode source, params string[] properties)
            {
                foreach (var item in properties)
                {
                    if (!source.HasProperty(item))
                    {
                        return false;
                    }
                }
            } 

        } 

  • Gareth Evans 142 posts 334 karma points c-trib
    Jan 12, 2012 @ 22:50
    Gareth Evans
    1

    I am pretty sure that HasValue will do what you need here

    If you want to check that "at least one" has a value:
    @if(Model.HasValue("propertyOne") || Model.HasValue("propertyTwo"))

    If you want to check both have a value:
    @if(Model.HasValue("propertyOne") && Model.HasValue("propertyTwo"))

    If you want to check one but not the other has a value (XOR)
    @if(Model.HasValue("propertyOne") ^ Model.HasValue("propertyTwo"))

    or, if you just want the value of propertyOne or propertyTwo you can use Coalesce. This is DynamicNull safe, so if the property doesn't exist on that node, it will fall back to the other case. Stack up as many properties as you want in Coalesce.

    @Library.Coalesce(Model.propertyOne, Model.propertyTwo)

  • mmmoustache 50 posts 79 karma points
    Jan 18, 2012 @ 18:32
    mmmoustache
    0

    Thanks for the input guys, massive props! The Umbraco version I'm using is fairly out of date so I ended up going with this, it works great:

     

    @{
        var @Model.HasProperty("propertyOne"&@Model.GetProperty("propertyOne").Value != String.Empty;
        var @Model.HasProperty("propertyTwo"&@Model.GetProperty("propertyTwo").Value != String.Empty;

     

        if((@x|(@y)){do something}
        else{do nothing}

     

Please Sign in or register to post replies

Write your reply to:

Draft