Copied to clipboard

Flag this post as spam?

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


  • jacob phillips 130 posts 372 karma points
    Jan 17, 2014 @ 02:01
    jacob phillips
    0

    Nested If statements

    Umbraco 6

    I noticed something that seems strange.... Within my partial view, I have a nested if statement:

    @{
    ...
    
        if(typedMNTPCollection.Any())
        {
             <div class="homeHighlightsCarouselOutter">
             if(size == "Small")
             {
    
                <div class="head headerColorHome with-rss">
                    <h3>@featureTitle</h3>
                  </div>
    
             }
            else {<p>its not</p>}
             </div>
        }
    }
    

    When rendered in a web page, the following text kept getting printed out to the page:

       if(size == "Small")
    

    Then I put an '@' in front of that if statement, and now it's working like I expect.

    I thought that once I'm inside of a code block starting with:

    @{
    

    ...that I don't have to preprend if statements with '@'

    Note that within the larger code block, I have other if statements that appear to be working just fine, without any preceding 'a':

    @{
        if(Model.Content.HasValue("cprHighlights"))
        {
            var typedMultiNodeTreePicker = new DynamicXml(Model.Content.GetPropertyValue<string>("cprHighlights"));
    
            var typedPublishedMNTPNodeList = new List<string>();
            foreach (dynamic id in typedMultiNodeTreePicker)                
            {
                typedPublishedMNTPNodeList.Add(id.InnerText); 
            }
            var typedMNTPCollection = Umbraco.TypedContent(typedPublishedMNTPNodeList).Where(x => x != null);
    
            foreach (var item in typedMNTPCollection)
            {     
                <p>@item.Name</p>         
                <p>@item.Id</p>         
            }   
    
    
            if(typedMNTPCollection.Any()) 
            {
                 <div class="homeHighlightsCarouselOutter">
                 @if(size == "Small")
                 {
    
                    <div class="head headerColorHome with-rss">
                        <h3>@featureTitle</h3>
                    </div> 
    
                 }
                else {<p>its not</p>}
                 </div>
            }
    
    
    
        }
    }
    

    What am I not understanding?

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jan 17, 2014 @ 08:15
    Dennis Aaen
    100

    Hi Jacob,

    The @ symbol is used in Razor initiate code, and tell the compiler where to start interpreting code, instead of just return the contents of the file as text. Using a single character for this separation, results in cleaner, compact code which is easier to read.

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

    The reason you experience that you if statement is just being outputted as plain text is because you have HTML markup in your code,

    and now you are out of razor. Therefor you need to to start the razor eigne again and tell the compiler where to start interpreting code by using the @ sign.

    Just like when you outputted your h3 heading.

    <divclass="head headerColorHome with-rss">
     
    <h3>@featureTitle</h3>
    </div>

    I hope this explanation why you need the @ is more clear to you now.

    /Dennis

  • Sam Ashton 4 posts 25 karma points
    Jan 17, 2014 @ 10:18
    Sam Ashton
    1

    Good Morning Jacob,

    It looks like your nested if statements are being interpreted as plain text because they are contained within HTML Markup, try prefixing your if with an @ symbol, for example:

    @{
    ...

       
    if(typedMNTPCollection.Any())
       
    {
             
    <div class="homeHighlightsCarouselOutter">
             @
    if(size =="Small")
             
    {

               
    <div class="head headerColorHome with-rss">
                   
    <h3>@featureTitle</h3>
                  </
    div>

             
    }
           
    else{<p>its not</p>}
             </
    div>
       
    }
    }

    Let me know if you have anymore issues.

    Thanks,

    Sam

  • jacob phillips 130 posts 372 karma points
    Jan 18, 2014 @ 03:04
    jacob phillips
    1

    Thanks Dennis for the reference and Sam for your comment. I was making assumptions about the compiler. I understand now.

Please Sign in or register to post replies

Write your reply to:

Draft