Copied to clipboard

Flag this post as spam?

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


  • Greg Berlin 818 posts 634 karma points
    Mar 23, 2012 @ 01:28
    Greg Berlin
    0

    Razor driving me crazy... hopefully a simple mistake

    I'm a newbie to Razor, so hopefully this is just a simple Razor rule i'm not aware of or something... So here goes:

    I'm trying to create a navigation based on one i found on the forum previously

    It works okay, but if i try to add my own conditionals (such as my logic for the home page), it breaks.  Every time.

    Is there a rule for Razor that i'm missing regarding conditionals?  Does anybody have a good link for a general intro to Razor that is relevant to the Umbraco context?

    My code:

    @*

    NESTED NAVIGATION WITH START AND FINISH LEVELS

    =======================================

    This snippet makes it easy to do navigation based lists! It'll automatically produce a nested list all children of a page within certain

    levels in the hierarchy that's published and visible (it'll filter out any pages with a property named "umbracoNaviHide"

    that's set to 'true'.

    Based on the inbuilt Navigation script in Umbraco 4.7                                                      

     

    How to Customize for re-use (only applies to Macros, not if you insert this snippet directly in a template):

    - If you add a Macro Parameter with the alias of "StartLevel" you can define the starting level for which nodes will be displayed

    - If you add a Macro Parameter with the alias of "FinishLevel" you can define the finish level for which nodes will be displayed                                                  

     

    *@        

     

    <div id="nav">

     

    @if (Parameter.AssignHomeManually == 1) {

      <ul id="topnav" class="sf-menu">

    <li><a href="/">HOME</a></li>

      </ul>

    }

     

    @nav(Model, Convert.ToInt32(Parameter.FinishLevel))

     

    </div>

     

                                                 

    @helper nav(dynamic node, int level) {

     

      var selected = Array.IndexOf(Model.Path.Split(','), node.Id.ToString()) >= 0 ? " class=\"current\"" : "";

     

      if(level > 0) {

        if(node.Children.Any()) {

          <ul id="topnav" class="sf-menu">

            @foreach(var child in node.Children.Where("Visible")) {

              <li>

                <[email protected](selected) href="@child.Url">@child.Name</a>

                @nav(child, level - 1)

              </li>

            }

          </ul>

        }

      }

    }

     

    Thanks heaps

    Greg

  • Greg Berlin 818 posts 634 karma points
    Mar 23, 2012 @ 02:48
    Greg Berlin
    0

    I should mention i'm using Umbraco 4.7.1.1

     

  • Rodion Novoselov 694 posts 859 karma points
    Mar 23, 2012 @ 04:35
    Rodion Novoselov
    0

    Hi. I guess that your "AssignHomeManually" macro parameter is of the "bool" type, and so you need to write

    @if(Parameter.AssignHomeManually == "1")

    instead of 

    @if(Parameter.AssignHomeManually == 1)

     

  • Greg Berlin 818 posts 634 karma points
    Mar 23, 2012 @ 04:55
    Greg Berlin
    0

    Okay... lets put this down to a learning curve issue.  I"ve fixed it now.. it basically took rewriting the thing about 5 times, each time improving and understanding more and more... 

    I'm not sure exactly what was wrong with the above code - I think it was a combination of various things.. here's what i came up with if anybody is interested (or cares to offer suggestions for improvement)?

     

    *@

    @inherits umbraco.MacroEngines.DynamicNodeContext

    @using umbraco.MacroEngines

     

    @{ 

        var startLevel = String.IsNullOrEmpty(Parameter.StartLevel) ? 2 : int.Parse(Parameter.StartLevel);

        var finishLevel = String.IsNullOrEmpty(Parameter.FinishLevel) ? 5 : int.Parse(Parameter.FinishLevel);

        dynamic homeNode = Model.NodeById(Parameter.HomeNode);

     

        <divid="nav">

          <ulid="topnav"class="sf-menu">

              @if (Parameter.AssignHomeManually == "1")

              {

                var selected = Model.Id == homeNode.Id ? " class=\"current\"" : "";

                <li><a @Html.Raw(selected) href="/">HOME</a></li>

              }

              @RenderChildren(Model.AncestorOrSelf().Children.Where("Visible"), false, startLevel)

          </ul>

        </div>

    }

     

    @helper RenderChildren(dynamic childNodes, bool isNested, int startLevel)

    {

        DynamicNode currentMenu = @Model.AncestorOrSelf(startLevel);

                

        Html.Raw(isNested ? "<ul>" : "");

        foreach (var node in childNodes)

        {

            var selected = Array.IndexOf(Model.Path.Split(','), node.Id.ToString()) >= 0 ? " class=\"current\"" : "";

        

            <li><a @Html.Raw(selected) href="@node.Url" title="@node.Name" >@node.Name</a></li>

            @RenderChildren(node.Children, true, startLevel)

        }

        Html.Raw(isNested ? "</ul>" : "");

    }

  • Greg Berlin 818 posts 634 karma points
    Mar 23, 2012 @ 04:56
    Greg Berlin
    0

    Whoa that was a bit weird.. seems copying from windows and pasting into mac does some strange stuff (ie: the colour scheme).

    Also there was no code formatting option in the forum.. strangeness

  • 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