Copied to clipboard

Flag this post as spam?

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


  • Richard Leonard 8 posts 38 karma points
    Oct 28, 2012 @ 14:47
    Richard Leonard
    0

    hierarchical menu issue

    I have written the following script which works without issue:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
      var level String.IsNullOrEmpty(Parameter.Levelint.Parse(Parameter.Level)
      var ulClass String.IsNullOrEmpty(Parameter.UlClass"" " class=\"current\""
      var parent @Model.AncestorOrSelf(level);
      if (parent != null
      {
       <ul class="menu">
           @if (parent.Id == Model.Id)
           {
             <li class="menu-item-selected"><href="@parent.Url">Home</a></li>
           }
           else
           {
             <li><href="@parent.Url">Home</a></li>
           }

       
           @foreach (dynamic item in parent.Children.Where("Visible").OrderBy("Order")
           {
            <li>
             if (item.Id == Model.Id)
             {
               <href="@item.Url">@item.Name</a>
             }
             else
             {
               <href="@item.Url">@item.Name</a>
             }  
            @if (item.Children.Count(0)
            {
             @traverse(item)
             }
        </li>   
           }
       </ul>
       }
    }

     

    However I was wanting to extend it the foreach loop to allow the class "menu-item-selected" to be added to the <li> tag.

    I have changed the script to be like this:

    @foreach (dynamic item in parent.Children.Where("Visible").OrderBy("Order")
           {
             if (item.Id == Model.Id)
             {
               <li class="menu-item-selected"><href="@item.Url">@item.Name</a>
             }
             else
             {
               <li><href="@item.Url">@item.Name</a>
             }  
            @if (item.Children.Count(0)
            {
             @traverse(item)
             }
        </li>   
           }

     

    However it won't let me save the script and states the issue "The code block is missing a closing "}" character".  Am I missing an @ somewhere, if so could somebody cast their eyes over it and advise where as i've been stumped for an hour now!

    Thanks 

  • Craig Noble 41 posts 584 karma points c-trib
    Oct 31, 2012 @ 01:03
    Craig Noble
    0

    In Razor you need to close a HTML tag inside the same code block.

    For example:

        @if(something == true)
    {
    <li><a href="index.html">Home</a>
    }
    </li>

       

    The above will break.

     

    However:

      @if(something == true)
    {
    <li><a href="index.html">Home</a></li>
    }

    The above will work.

     

    If you want to output a tag but don't want the open or close tag to break the code, then do:

    @Html.Raw("<li>")

     

     

    Any questions let me know.

Please Sign in or register to post replies

Write your reply to:

Draft