Copied to clipboard

Flag this post as spam?

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


  • Dominic Kelly 114 posts 133 karma points
    Jan 24, 2012 @ 14:54
    Dominic Kelly
    0

    Childsplay - if else?????

    I'm finding Umbraco's syntax disgusting verbose. I've been trying to an if else statement in a macroscript for half an hour and absolutely loosing the will to live. 

    I've got a custom property against a document type called showSlideshow which can either be true or false.

    @if (@Model.showSlideshow == "") {
    } else

    OBVIOUSLY fails and errors. I have no idea why.

    Please tell me how to do an if else statement in a .cshtml file. I just want to be able to copy and paste it in so it will work. 

    I basically want to show the following if it's true:

     <div class="slider-wrapper">
     
       <div id="slider">
          @{var imgid1 = @Model.innerLandingSlide1Background;}
          @{var imgid2 = @Model.innerLandingSlide2Background;}
          @{var imgid3 = @Model.innerLandingSlide3Background;}
          <img src="@Model.MediaById(imgid1).umbracoFile" width="938" height="249" alt="@Model.MediaById(imgid1).Name" /> 
          <img src="@Model.MediaById(imgid2).umbracoFile" width="938" height="249" alt="@Model.MediaById(imgid2).Name" /> 
          <img src="@Model.MediaById(imgid3).umbracoFile" width="938" height="249" alt="@Model.MediaById(imgid3).Name" /> 
        </div>
        
        <div class="slider-content">
          <ul class="clearfix">
            <li id="content_0">
              <p class="title">@Model.innerLandingSlide1Heading</p>
              <p class="sub">@Model.innerLandingSlide1Copy</p>
            </li>
            <li id="content_1">
              <p class="title">@Model.innerLandingSlide2Heading</p>
              <p class="sub">@Model.innerLandingSlide2Copy</p>
            </li>
            <li id="content_2">
              <p class="title">@Model.innerLandingSlide3Heading</p>
              <p class="sub">@Model.innerLandingSlide3Copy</p>
            </li>
          </ul>
        </div>
        <div class="slider-links">
          <ul class="clearfix">
              @{
              var link1 = @Library.NodeById(Model.innerLandingSlide1Link);
              var link2 = @Library.NodeById(Model.innerLandingSlide2Link);
              var link3 = @Library.NodeById(Model.innerLandingSlide3Link);
              <li class="selected"><a id="slide_0" href="@link1.Url">@link1.Name</a></li>
              <li><a id="slide_1" href="@link2.Url">@link2.Name</a></li>
              <li><a id="slide_2" href="@link3.Url">@link3.Name</a></li>
              }
          </ul>
        </div>
    </div>

     

    Or another div if it's false.

    Quite frankly I'm embarresed to be asking how to do such a remedial tasks, but it just goes to show my desecion to leave all Microsoft technologies behind was well founded. This time I really do mean 'never again'.

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jan 24, 2012 @ 14:59
    Lee Kelleher
    0

    Hi Dominic, I hear your frustration!

    I haven't used Razor much (more in the XSLT camp), but still I want to help.

    Think you can get rid of the @ symbol before the "@Model" - because you are already in an "if" condition.

    @if (Model.showSlideshow == "")

    Hope that helps?

    Cheers, Lee

  • Dominic Kelly 114 posts 133 karma points
    Jan 24, 2012 @ 15:00
    Dominic Kelly
    0
    @if (Model.showSlideshow == "this")
    {
     
    } else
    }

     

    Produces:

    "Error loading MacroEngine script (file: InnerLandingSlides.cshtml)"
     
  • Dominic Kelly 114 posts 133 karma points
    Jan 24, 2012 @ 15:02
    Dominic Kelly
    0

    @if ("this" == "this")

    {

     

    } else

     

    }

    doesn't error

     

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jan 24, 2012 @ 15:03
    Lee Kelleher
    0

    Hi Dominic - quick check, which version of Umbraco are you using? v4.7.1 or v4.7.1.1 (aw, so many point-1s) ;-)

  • Dominic Kelly 114 posts 133 karma points
    Jan 24, 2012 @ 15:05
    Dominic Kelly
    0

    4.7.1.1

  • Dominic Kelly 114 posts 133 karma points
    Jan 24, 2012 @ 15:06
    Dominic Kelly
    0

    @{var show = @Model.showSlideshow;}

     

    @if (show == "True")

    {

        <p>1</p>

    }

    else

    <p>2</p>

    }

     

    Another fail. WTF.

  • Dominic Kelly 114 posts 133 karma points
    Jan 24, 2012 @ 15:11
    Dominic Kelly
    0

    @{var show = @Model.showSlideshow;}

     

    @if ("@show" == "True")

    {

        <p>1</p>

    }

    else

        <p>2</p>

    }

     

    FAIL

  • Dominic Kelly 114 posts 133 karma points
    Jan 24, 2012 @ 15:13
    Dominic Kelly
    0

    @{

     

    var show = @Model.showSlideshow;

        

    if (@show == "True")

    {

        <p>1</p>

    }

    else

        <p>2</p>

    }

     

    }

    FAIL

  • Dominic Kelly 114 posts 133 karma points
    Jan 24, 2012 @ 15:14
    Dominic Kelly
    0

    @{

     

    var show = @Model.showSlideshow;

        

    if (@show)

    {

        <p>1</p>

    }

    else

        <p>2</p>

    }

    }

    PASS. Jesus H Christ. 

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jan 24, 2012 @ 15:18
    Lee Kelleher
    0

    Hi Dominic,

    It's because the value of "Model.showSlideshow" is strongly-typed ... since the property is a true/false checkbox, the value would be a boolean.  So when the script tries to compare a boolean to a string, the Razor engine/compiler borks! :-(

    Things that would work are...

    @if (Model.showSlideshow == true)
    @if (Model.showSlideshow)

    Frustrating, I know - you're not alone. :-)

    Cheers, Lee.

  • Dominic Kelly 114 posts 133 karma points
    Jan 24, 2012 @ 15:20
    Dominic Kelly
    0

    Right ok... well now I know, 1 hour later! Thanks.

  • Rodion Novoselov 694 posts 859 karma points
    Jan 24, 2012 @ 15:25
    Rodion Novoselov
    1

    You even don't need a redundant variable. Simply "@if(Model.showSlideshow) { ... " should work ok. Initial errors caused as I can guess by that the "showSlideshow" property returns a boolean runtime value. The compiler allows comparing a dynamic-typed expression to any both dynamic- and static- typed one but doesn't perform any type conversion to cast both hands to the same runtime type. So such an expression just fails in runtime as soon as being executed.

  • Rodion Novoselov 694 posts 859 karma points
    Jan 24, 2012 @ 15:26
    Rodion Novoselov
    0

    Ughm. I've posted my message a bit late. :-)

  • Salman Ansari 7 posts 27 karma points
    Nov 17, 2012 @ 12:16
    Salman Ansari
    0

    Thank you so much....code save my lot of time..

    Thanks Again

  • William Charlton 171 posts 218 karma points
    Feb 06, 2013 @ 18:38
    William Charlton
    0

    I too am singularly unimpressed with razor, the bastard child of c and xsl. I have a fairly simple piece of code from http://daniel.streefkerkonline.com/tagging-and-umbraco/

    One small thing he overlooked was that if one item is returned the output says "1 items were..." I want to add an if else block to catch the count and output the correct English, along the lines of:

    ===
    @inherits umbraco.MacroEngines.DynamicNodeContext
    @using System.Text
    @using umbraco.MacroEngines
    @using umbraco.cms.businesslogic.Tags

    @{  
        string searchFor = Request["tags"];
     
        if(string.IsNullOrEmpty(searchFor))
        {
            @* No tags were specified *@
            <p>Please specify a tag to search for</p>
            return;
        }
     
        var matchingNodes = Tag.GetNodesWithTags(searchFor).ToList();

        string tagsText = searchFor.Split(',').Count() > 1 ? "tags" : "tag";
       
        if (matchingNodes.Count < 1)
        {
           @* No results were found for the specified tags *@
           <p>No tagged items were found that matched the @tagsText: @searchFor</p>
           return;       
        }
     
         @* Some results were found for the specified tags *@
         <p><strong>@matchingNodes.Count</strong>


    @* output the correct text*@

            if (matchingNodes.Count = 1)
        {
             "item was";
        }
        else
        {
            "items were";
        }
             
             found that matched the @tagsText: "@searchFor"</p>
     
          <ul>
          @foreach (var node in matchingNodes)
          {
            dynamic dn = new DynamicNode(node.Id);
     
            <li><a href="@dn.Url">@dn.Name</a></li>
          }
          </ul>
    }

    ===

    Anyone?

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Feb 06, 2013 @ 18:53
    Tom Fulton
    0

    Hi William,

    Since you're in code block where you want to write out the text, you have to tell Razor you want to output this text rather than interpreting it as code.  Here's a few different appraoches:

     

    if (matchingNodes.Count = 1)
    {
            <span>item was</span>
    }

    Or, 

    if (matchingNodes.Count = 1)
    {
            @: item was
    }

    Or,

    if (matchingNodes.Count = 1)
    {
            <text>item was</text>
    }

    Check out this post for more info:  Razor Tip #2: Output Text in Code Context

    Hope this helps,
    Tom 

     

  • William Charlton 171 posts 218 karma points
    Feb 06, 2013 @ 19:11
    William Charlton
    0

    Thanks Tom, I gathered it was something do do with context and letting Razor know what it is expected to do. Because I am already in a text block I had to add @ to the if, and had to double the = as otherwise Razor thought I was trying to assign a value, so I finally got:

     @if(matchingNodes.Count == 1)
        {
             <text>item was</text>
        }
        else
        {
            <text>items were</text>
        }

     

    Which does the job a treat :)

Please Sign in or register to post replies

Write your reply to:

Draft