Copied to clipboard

Flag this post as spam?

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


  • René Andersen 238 posts 684 karma points
    Aug 21, 2014 @ 12:32
    René Andersen
    0

    Topnavigation problem

    Hello everybody

    I need your help to something I think is really simple. Just not for me. :-)

    In the code below I have made a TopNavigation that works. But as you can see then the "Home" menu item is hardcoded / static and I really want it to do the same as the submenu. When I create the submenu I use "Foreach" because there will be more than one submenu.

    My question is - How can I make "Home" to be dynamic instead of hardcoded / static without the use of "Foreach" because it is only one menu item.?

    Please add to the code below if you have the solution.

    Thank you in advance!

    //René

    <nav>
    <ul class="navigation">
    <li>
    <a href="/">
    <span class="label-nav">
    <span class="@(CurrentPage.Url == "/" ? "current_page_item" : null)">
    Forside
    </span>
    </span>
    <span class="label-nav-sub" data-hover="Vores forside">
    Vores forside
    </span>
    </a>
    </li>
    @foreach (var item in menuItems)
    {
    <li>
    <a href="@item.Url">
    <span class="label-nav">
    <span class="@(CurrentPage.Id == item.Id ? "current_page_item" : null)">
    @item.menuName
    </span>
    </span>
    <span class="label-nav-sub" data-hover="@item.menuSubName">
    @item.menuSubName
    </span>
    </a>
    </li>
    }
    </ul>
    </nav>

     

  • Jason Espin 368 posts 1335 karma points
    Aug 21, 2014 @ 14:40
    Jason Espin
    100

    I may have misunderstood the question but from the sound of things you want to display the homepage of the site but without hard coding it. I'd assume this is so that you can pull through the correct name for the homepage. The way in which I do this is using the following code snippet:

    IPublishedContent homepage = Model.Content.AncestorsOrSelf(1).First();
    

    As your homepage should be the first node in your document tree, this will select it so that you can use it how you wish.

    The way is works is, Model.Content selects the current page that you are on. AncestorsOrSelf(1) will select any ancestors of the current page that are on level 1. If the page itself is on level one it will also be selected. At this point you have an IEnumerable

    You can then use :

    @homepage.Name
    

    and

    @homepage.Url 
    

    to access the relevant properties needed to create the link.

  • René Andersen 238 posts 684 karma points
    Aug 22, 2014 @ 09:18
    René Andersen
    0

    Hi Jason

    That worked perfectly. But is there a way to use "@homepage.menuName" instead of "@homepage.Name". menuName is something I have made, so that you can change the name from e.g. a content tab and also use letters like "æøå".

    Thanks!

    //René

  • dillorscroft 198 posts 192 karma points
    Aug 22, 2014 @ 10:11
    dillorscroft
    1

    Hi,

    You can simply use @homepage.GetPropertyValue("menuName")

    Matt

  • René Andersen 238 posts 684 karma points
    Aug 22, 2014 @ 11:01
    René Andersen
    0

    Thanks Matt

    That worked like a charm. :-)

    // René

Please Sign in or register to post replies

Write your reply to:

Draft