Copied to clipboard

Flag this post as spam?

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


  • Tom Cowling 144 posts 342 karma points
    Nov 20, 2013 @ 15:09
    Tom Cowling
    0

    Simple razr if statement

    Hi everyone,

    I'm trying to run a simple if test in Razr, but I'm not familiar with the language so wondered if anyone could help.

    What I'm looking to do is the following:

    If a field is empty, don't display it - simple right!

     

    Here's what I have so far, but I don't know where to start as I'm getting a runtime error..

    <umbraco:Macro runat="server" language="cshtml">
    @if(Model.GetProperty("heading").Value!=String.Empty){
    <h1><umbraco:Item field="heading" runat="server" /></h1>
    }
    </umbraco:Macro>

     

    Any help would be much appreciated!

     

    Thanks,

    Tom

  • Jeavon Leopold 3008 posts 13221 karma points MVP 7x admin c-trib
    Nov 20, 2013 @ 15:12
    Jeavon Leopold
    100

    Hi Tom

    Give this a try:

    <umbraco:Macro runat="server" language="cshtml">
    
    @if(Model.HasValue("heading")){
       <h1>@Model.heading</h1>
    }
    </umbraco:Macro>
    

    Jeavon

  • Tom Cowling 144 posts 342 karma points
    Nov 20, 2013 @ 15:14
    Tom Cowling
    0

    Perfect thanks a lot!

    So is the hasvalue statement a shorthand for checking if something exists in the page as it's being built?

    Could anyone recommend a good solid starting place to learn Razr too?

     

    Thanks!

    Tom

  • Dennis Aaen 4457 posts 17970 karma points admin hq c-trib
    Nov 20, 2013 @ 15:19
    Dennis Aaen
    0

    Hi Tom,

    A goog starting point could be:

    http://our.umbraco.org/documentation/Reference/Templating/Macros/Razor/basic-razor-syntax

    http://our.umbraco.org/documentation/reference/templating/macros/razor

    And the Umbraco TV.

    http://umbraco.tv/

    The documentation for working with some you the dbuilt-in-property-editors:

    http://our.umbraco.org/documentation/using-umbraco/backoffice-overview/property-editors/built-in-property-editors/

    Hope this can helps you and of course keep asking in the forum.

    /Dennis

  • Jeavon Leopold 3008 posts 13221 karma points MVP 7x admin c-trib
    Nov 20, 2013 @ 15:26
    Jeavon Leopold
    1

    Hi Tom,

    Yes, to be really really sure you can do this which will first check the property exists and then check if it has a value

    @if(Model.HasProperty("heading") && Model.HasValue("heading")){
       <h1>@Model.heading</h1>
    }
    

    Are you just starting out with this project/Umbraco? If you are, I would recommend that you use Mvc templating rather than WebForms? The DynamicNode object you are using here (Model) is now legacy and has been fully replaced by IPublishedContent/UmbracoHelper either native in Mvc views or using a Partial View Macro with a WebForms (.master) template.

    I would also recommend umbraco.tv as Dennis has above.

    Jeavon

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies