Copied to clipboard

Flag this post as spam?

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


  • René Andersen 238 posts 684 karma points
    May 26, 2015 @ 15:16
    René Andersen
    0

    Insert an (if) statement in Partial View Macro

    Hi,

    How is it possible to insert a (if) statement in a macro? I have tried the following without luck:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage

    <div class="work-details">
    <h5 class="work-details-title font-alt">@Model.MacroParameters["title"]</h5>
    <ul>
    @if (@Model.MacroParameters["date"]) {
    <li><strong>Date:</strong><span class="font-serif"> @Model.MacroParameters["date"]</span></li>
    }
    <li><strong>Location:</strong><span class="font-serif"> @Model.MacroParameters["location"]</span></li>
    <li><strong>Performers:</strong><span class="font-serif"> @Model.MacroParameters["performers"]</span></li>
    </ul>
    </div>

    Thanks in advance!

    // René

  • René Andersen 238 posts 684 karma points
    May 26, 2015 @ 15:19
    René Andersen
    0

    Sorry wrong code, this code below is the correct one. But it still does not work.

    @if (@Model.MacroParameters.HasValue["date"])
  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    May 26, 2015 @ 19:17
    Dennis Aaen
    0

    Hi René

    I think this code below should work for you, if you already have add the parameter to the macro.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
        <div class="work-details">
            <h5 class="work-details-title font-alt">@Model.MacroParameters["title"]</h5>
            <ul>
                @if (Model.MacroParameters["date"] != null){
                <li><strong>Date:</strong><span class="font-serif"> @Model.MacroParameters["date"]</span></li>
                }
                <li><strong>Location:</strong><span class="font-serif"> @Model.MacroParameters["location"]</span></li>
                <li><strong>Performers:</strong><span class="font-serif"> @Model.MacroParameters["performers"]</span></li>
            </ul>
        </div>
    

    Hope this helps,

    /Dennis

  • René Andersen 238 posts 684 karma points
    Aug 19, 2015 @ 10:39
    René Andersen
    0

    Hi Dennis

    I have tried the code below. But it shows the Facebook icon every time with or without data in the textbox field "facebookTest".

            @if (Model.MacroParameters["facebookTest"] != null)
        { 
        <li class="about-social-links"><a href="@Model.MacroParameters["facebookTest"]" target="_blank"><i class="fa fa-facebook-square fa-3x"></i></a></li>
        }
    

    Have I done something wrong?

    // René

  • Lars-Erik Aabech 349 posts 1100 karma points MVP 7x c-trib
    Aug 19, 2015 @ 11:09
    Lars-Erik Aabech
    2

    The parameter is probably not null, it's probably "".
    You can use the static methods String.IsNullOrEmpty or String.IsNullOrWhiteSpace, or the respective extension built in to Umbraco:

    @if (!String.IsNullOrEmpty(Model.MacroParameters["facebookTest"]))
    

    or

    @if(!Model.MacroParameters["facebookTest"].IsNullOrWhiteSpace())
    

    (I guess the difference gives itself away :) )

  • René Andersen 238 posts 684 karma points
    Aug 19, 2015 @ 11:23
    René Andersen
    0

    Hi Lars-Erik

    Thanks!

    I tried copy in your code but I get this error "the best overloaded method match for 'string.isnullorempty(string)' has some invalid arguments".

    Any idea what's wrong?

    Anyway, your answer kinda helped me because I tried to edit the code to this:

            @if (Model.MacroParameters["facebookTest"] != "")
    

    Can I go on with my editet code or is it a bad idea?

    // René

  • Lars-Erik Aabech 349 posts 1100 karma points MVP 7x c-trib
    Aug 19, 2015 @ 11:30
    Lars-Erik Aabech
    101

    Ah, yes. The MacroParameters indexer returns objects.
    As long as the facebookTest parameter stays a string (you can have other types), you can safely cast it so it matches the method parameter.

    @if (!String.IsNullOrWhiteSpace((string)Model.MacroParameters["facebookTest"]))
    

    The reason I like to use that one instead of != "" or != null is that empty values tend to be pretty volatile depending on the status of the owning object.

    One day you think they're always null, the next day you discover some user managed to make just the one "", and a year from now, some junior developer manages to fill the database with whitespaces. ;)

  • René Andersen 238 posts 684 karma points
    Aug 19, 2015 @ 11:39
    René Andersen
    0

    Thank you for your answer!

    Your code now works perfectly. :-)

  • Lars-Erik Aabech 349 posts 1100 karma points MVP 7x c-trib
    Aug 19, 2015 @ 11:43
    Lars-Erik Aabech
    1

    Happy to help. :)

    Keep in mind that other data types for other parameters might be returned as other objects than string. Those might be anything from null to "null objects" to "crap data" or 0.

Please Sign in or register to post replies

Write your reply to:

Draft