Copied to clipboard

Flag this post as spam?

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


  • Sebastian Dammark 583 posts 1407 karma points
    Apr 27, 2013 @ 17:46
    Sebastian Dammark
    0

    Howto: Get Ancestor at level 2 of currentpage ?

    How do I do this

    $currentPage/ancestor-or-self::*[@level = 2 and not(umbracoNaviHide=1)]/@nodeName

    In Razor ?

  • Charles Afford 1163 posts 1709 karma points
    Apr 27, 2013 @ 18:39
    Charles Afford
    0

    Hi, What is umbracoNaviHide?  Is that a property?  What type is it?

    DynamicNode rootNode = new DynamicNode(-1); // right at the top of the tree

    DynamicNode HomeNode = rootNode(-1); //the first node in my tree

    //Then do a foreach

    Foreach(DynamicNode ctxNode = RootNode.Descendants().Where(x=>x.Level == 2 && x.getProperty("umbracoNaviHide).Value == 1)

    {

    Do something with your nodes :).

    }

  • Sebastian Dammark 583 posts 1407 karma points
    Apr 27, 2013 @ 19:38
    Sebastian Dammark
    0

    Dang ... that was a lot of code for replacing an XSLT one liner.
    Guess that's why I'm still crawling back to XSLT :)

    umbracoNaviHide is, or should be, property of the type boolean.

    But I don't know if it's the case in Razor, but in the Umbraco XML a boolean can be 1, 0 or "" (empty), which is why I use the not(umbracoNaviHide=1).

     

  • Sebastian Dammark 583 posts 1407 karma points
    Apr 27, 2013 @ 19:39
    Sebastian Dammark
    0

    And by the way.

    Why use a foreach when it only returns a single node ?

  • gary 385 posts 916 karma points
    Apr 27, 2013 @ 19:48
    gary
    0

    Hi Sebastian

    Don't know XSLT but from what I can see of it, how about

    @CurrentPage.AncestorsOrSelf(2).Where("Visible").Name

    umbraco NaviHide is used because it returns on Visible as above.

    So current page up 2 levels, not hidden from navigation, node name, is what it should return.

    Gary

  • Sebastian Dammark 583 posts 1407 karma points
    Apr 27, 2013 @ 19:53
    Sebastian Dammark
    0

    I get this error when I try to save the file within the Umbraco backoffice

    The name 'CurrentPage' does not exist in the current context

  • Sebastian Dammark 583 posts 1407 karma points
    Apr 27, 2013 @ 19:56
    Sebastian Dammark
    0

    If I skip testing I don't get anything ...

  • gary 385 posts 916 karma points
    Apr 27, 2013 @ 19:58
    gary
    0

    Ok

    What version of Umbraco? Webforms or MVC?

    or try @Model.AncestorsOrSelf etc. . . 

    If that does not compile, will need version etc to help more

    G

  • Sebastian Dammark 583 posts 1407 karma points
    Apr 27, 2013 @ 19:58
    Sebastian Dammark
    0

    Gary: Question.

    The 'AncestorsOrSelf(2)' does go to level 2 or does it go up 2 levels from the level of currentPage ?

     

  • Charles Afford 1163 posts 1709 karma points
    Apr 27, 2013 @ 19:58
    Charles Afford
    0

    Yea thats a good point that was from a model i had.  If you are in a razor view then you could do @Model....

    You dont need to do a foreach for a single node.

    At the top of your razor page: @inherits Umbraco.Web.Mvc.UmbracoTemplatePage

    Then:

    int umbracoNaviHide = 0;

    Int32.Parse(@Model.Context.getProperty("umbracoNaviHide").Value.ToString(), out umbracoNaviHide); parse the bool value

    DynamicNode rootNode = new DynamicNode(-1); // right at the top of the tree

    string myvalue =  rootNode.Descendants().Where(x=>x.Level == 2 && umbracoNaviHide == 1)

    Something like that :)

    }

  • Charles Afford 1163 posts 1709 karma points
    Apr 27, 2013 @ 20:01
    Charles Afford
    0

    Seb, just go from the top and work down.  If you need to implement more functionality in the future of tweek what nodes you are finding.  That will be the simple way imo

  • Sebastian Dammark 583 posts 1407 karma points
    Apr 27, 2013 @ 20:21
    Sebastian Dammark
    0

    Gary:  I'm using Umbraco v6.0.3 webforms

  • gary 385 posts 916 karma points
    Apr 27, 2013 @ 20:34
    gary
    0

    Hi

    I use MVC,

    but you should be able to use @Model.Content.AncestorsOrSelf(2).Where("Visible").Name (if you take this route in WebMatrix or VS, you should get Intellisense)

    This will return node name at level 2. If you need to go up 2 levels, @Model.Content.Up(2).Name will return it.

    Check your @Using statement at the top of the page, it should be @Umbraco.MacroEngines or something like that, sorry as I say I use MVC.

    You can also have a look at this razor walkthrough, it is still valid and should help you.

    http://umbraco.com/follow-us/blog-archive/2011/2/23/umbraco-47-razor-feature-walkthrough-%E2%80%93-part-1.aspx 

    Best regards

    Gary

  • Sebastian Dammark 583 posts 1407 karma points
    Apr 28, 2013 @ 00:40
    Sebastian Dammark
    0

    Uhhh ... WebMatrix or VS ??

    I'm on a mac, so I ain't going there :)
    But my file has '@inherits umbraco.MacroEngines.DynamicNodeContext' in the top.

    I've tried

    @Model.Content.AncestorsOrSelf(2).Where("Visible").Name

    and 

    @Model.Content.Up(2).Name

    But none of them gave me any output.

    I think I'll leave the Razor stuff to the backend developers.
    But thanx alot for your help guys. 

  • Bjørn Fridal 274 posts 784 karma points
    Sep 22, 2013 @ 20:17
    Bjørn Fridal
    0

    Hi

    I know this is an old one, but I thought I would leave a comment for future readers, since I just had the exact same problem. I tried to do:

    @Model.Content.AncestorsOrSelf(2).Where("Visible").Name

    and it didn't work. Just gave me an error. Then it occurred to me that I had written "Ancestors" - plural. It should of course by singular. 

    @Model.Content.AncestorOrSelf(2).Where("Visible").Name

    Cheers
    Bjørn 

Please Sign in or register to post replies

Write your reply to:

Draft