Copied to clipboard

Flag this post as spam?

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


  • Roey 5 posts 25 karma points
    Feb 20, 2013 @ 13:30
    Roey
    0

    how to get the deepest level child of Home page

    Hi I'm new to Umbraco Razors

    Please help me to resolve this..

    when I use  Model.Descendants(1).Last(); it is giving the last node of the home page but not the lowest level


    for eg:  I have a n-level navigation (n not known) and I want to display the navigation tree only for 3 levels if n value is greater.

    how can i get the value of n

     

    I tried to get the deepest level fo the below navigation structure and failed to get the correct result

    the code i wrote is

    @{ 

    var deepest = Model.descendants(1).Last();

     

    Level::  @deepest.Level

     

     

    PageName::  @deepest.Name

     

    }

    and the result is 

    Level:: 3 (which is not the deepest level)

    PageName:: level 3-5

     

    Any help would be great

    thanks in advance

    Roey

  • gary 385 posts 916 karma points
    Feb 20, 2013 @ 18:45
    gary
    0

    Hi Roey

    Sorry for late reply. If I understand what you would like to do, then this code will return you a navigation down to level 3

      @foreach(var page in @Model.Content.AncestorOrSelf().Descendants().Where("level<5"))

      {      

            <a href="@page.Url">@page.Name</a>

    }

    I have left level at 5, but if you adjust you will see what returns, it is worth to note that level 1 is 0, so sometimes can be a little confusing.

    Hope this helps to get you started, but you may have to play a little, I use V6 Mvc version, so you may need to check your @using statements at the top of your file if you are using Webforms.

    Hope it helps Gary

  • Charles Afford 1163 posts 1709 karma points
    Feb 21, 2013 @ 23:37
    Charles Afford
    0

    The  ().Where("level<5")) is an optional constraint if you take this off it will keep searching down the tree for you :).  Charlie :)

  • Roey 5 posts 25 karma points
    Feb 22, 2013 @ 11:02
    Roey
    0

    hi GARY and CHARLIE,

     

    thanks for your response and suggestions,

    i didn't want to put any particular number in where(). I got what i wanted with the following code but thats not completely dynamic too.

    @inherits umbraco.MacroEngines.DynamicNodeContext

    @{ 

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

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

      var parent = @Model.AncestorOrSelf(startLevel);

      if (parent != null) { @traverse(parent,startLevel,finishLevel) ; }

    }                                                     

                                                       

    @helper traverse(dynamic parent,int startLevel,int finishLevel)

    {

     <ul [email protected]>

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

          var selected = Array.IndexOf(Model.Path.Split(','), node.Id.ToString()) >= 0 ? "class = selected"+" "+"current-"[email protected] : "";

           

    <li @Html.Raw(selected)> 

     

            <a href="@node.Url">@node.Name</a>

     

            @if (selected!=""&&@node.Level<=finishLevel) { @traverse(node,startLevel,finishLevel); }  

          </li>

          }

      </ul>                                                            

    }

     

  • Roey 5 posts 25 karma points
    Feb 22, 2013 @ 11:09
    Roey
    0

    I also have other query

    i'm dynamically generating classes for the li items for example  (<li class="selected current-2">)

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

          var selected = Array.IndexOf(Model.Path.Split(','), node.Id.ToString()) >= 0 ? "class = selected"+"  "+"current-"[email protected] : "";

      <li @Html.Raw(selected)> 

     

    but the space after "selected" is messing up my class names

     

    Thanks for any help

    Rohith


  • Charles Afford 1163 posts 1709 karma points
    Feb 23, 2013 @ 21:56
    Charles Afford
    0

    Hi Roey.  Sorry for the late reply.  Surly in the start and stop clause

     

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

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

    dont you want to add a clause here to say either

    where there are no parents (must be the top)

    where there are no children (must be the bottom)

     

     

  • Roey 5 posts 25 karma points
    Feb 27, 2013 @ 14:18
    Roey
    0

    Hi Charles,

    thanks for you help with the previous one..

     

    I've a new one now...

    in the above tree all nodes are of same document type (umbTextpage)

    I want to find the rootnode (with same doctype) of current page

    I tried

    var parent = @Model.AncestorOrSelf("umbTextpage");

    but that only returns the self page, so i tried

    Model.Ansestors("umbTextpage"); and still no use

     

    is there any way I can find the top level ancestor with "umbTextpage" doctype

    Does XPath work in this case, if it works then HOW?

     

    Thanks

    Roey

     

Please Sign in or register to post replies

Write your reply to:

Draft