Copied to clipboard

Flag this post as spam?

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


  • Claushingebjerg 886 posts 2415 karma points
    Oct 09, 2014 @ 11:34
    Claushingebjerg
    0

    Why do "elements" have to contain html?

    I have an issue, that keeps coming back in every project i work on, and now i really need to know to to solve it for good.

    For some reason whenever i have a razor context, it has to have html inside, or else it fails. Example:

    @foreach (var page in startNode.Children.Where("Visible")) { 
      <a href="@Umbraco.Media(page.logo).Url">
    @if (page.HasValue("logo")){ <img src="@Umbraco.Media(page.logo).Url"/> } </a>
    }

    Works fine, but it's not really what i want. I want the intire link and image inside the if

    @foreach (var page in startNode.Children.Where("Visible")) { 
         @if (page.HasValue("logo")){
          <a href="@Umbraco.Media(page.logo).Url">
            <img src="@Umbraco.Media(page.logo).Url" />
            </a>
        }
    }

    But this fails... Why?

    And how do i get it right. I often run into similar cases where stuff fails due to empty "elements".

  • Claushingebjerg 886 posts 2415 karma points
    Oct 09, 2014 @ 11:51
    Claushingebjerg
    0

    I think i figured it out... You cant have a @ inside some other @.

    So it needs to be

    @foreach(var page in startNode.Children.Where("Visible")){ 
         
    if(page.HasValue("logo")){
          <a href="@Umbraco.Media(page.logo).Url">
            <img src="@Umbraco.Media(page.logo).Url"/>
            </a>
        }
    }

    Could someone point to a thorough explantion to how these @ indicators work?

  • Ali Sheikh Taheri 470 posts 1647 karma points c-trib
    Oct 09, 2014 @ 11:55
    Ali Sheikh Taheri
    0

    Hi

    I believe if you just remove the @ sign before if , it should sort it out

    Cheers

    Ali

  • Steve Morgan 1278 posts 4216 karma points c-trib
    Oct 09, 2014 @ 12:19
    Steve Morgan
    0

    It helps me to think of "modes" - when you write a @{ or begin a code bloc @foreach you're in razor code "mode" until you throw a line of HTML in which switches the mode to HTML. 

    I sometimes use the @: marker to output a "quick" line of HTML whilst still retaining the Razor code mode. So you can do things like:

     

     @foreach(var page in startNode.Children.Where("Visible")){
    @: <h1>Perhaps some debug output @page.Id</h1>
         if(page.HasValue("logo")){
          <a href="@Umbraco.Media(page.logo).Url">
            <img src="@Umbraco.Media(page.logo).Url"/>
            </a>
        }
    }
  • 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