Copied to clipboard

Flag this post as spam?

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


  • Aly Sebastien 19 posts 36 karma points
    Jan 01, 2014 @ 02:26
    Aly Sebastien
    0

    Trouble creating a navigation menu to include the parent node and the child nodes

    I have a basic site with the content tree as follows;

    Homepage
    > About Us
    > Products

    The xslt macro creates a menu but only lists the child nodes. I could hard code the homepage but that seems to be a hacky approach (not included in code below).

        <xsl:param name="currentPage"/>

    <!-- Input the documenttype you want here -->
    <xsl:variable name="level" select="1"/>

    <xsl:template match="/">

    <!-- The fun starts here -->
    <ul>
            
    <xsl:for-each select="$currentPage/ancestor-or-self::* [@level=$level]/* [@isDoc and string(umbracoNaviHide) != '1']">
        <li>
            <a href="{umbraco.library:NiceUrl(@id)}">
                <xsl:value-of select="@nodeName"/>
            </a>
        </li>
    </xsl:for-each>
    </ul>

    </xsl:template>

    </xsl:stylesheet>



    However, is it better to use cshtml?

        @inherits umbraco.MacroEngines.DynamicNodeContext

    @*
        Macro to display child pages below the root page of a standard website.
        Also highlights the current active page/section in the navigation with
        the css class "current".
    *@


    @{
        @*Get the root of the website *@
        var root = Model.AncestorOrSelf(1);
    }

    <ul>
        <li><a href="@root.NiceUrl">@root.Name</a></li>
        @foreach (var page in root.Children.Where("Visible"))
        {
            
            <li class="@page.IsAncestorOrSelf(Model, "current", "")">
                <a href="@page.Url">@page.Name</a>
            </li>
        }
    </ul>


    This would add a class of current to the li of the page I am on.

    Seems odd how you are going to need a website with a main navigation using the recommended content structure but there seems to be no clear or accessible solution?

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jan 01, 2014 @ 16:27
    Dennis Aaen
    0

    Hi Aly,

    Try this code:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:msxml="urn:schemas-microsoft-com:xslt"
        xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" xmlns:Examine="urn:Examine" xmlns:google.maps="urn:google.maps"
        exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets Examine google.maps ">

    <xsl:output method="xml" omit-xml-declaration="yes"/>
          

     <xsl:param name="currentPage"/>

            <!-- Input the documenttype you want here -->
            <xsl:variable name="level" select="1"/>
           
            <!-- Root Node -->
            <xsl:variable name="rootNode" select="$currentPage/ancestor-or-self::root" />

            <!-- Homepage -->
            <xsl:variable name="homeNode" select="$rootNode/Frontpage[@isDoc]" />

    <xsl:template match="/">

    <!-- The fun starts here -->
    <ul>
    <!--Create a link to the homepage -->

        <li>
        <xsl:if test="$homeNode/@id = $currentPage/@id">
                <xsl:attribute name="class">selected</xsl:attribute>
             </xsl:if>
        <a href="{umbraco.library:NiceUrl($homeNode/@id)}">
            <xsl:value-of select="$homeNode/@nodeName" />
        </a>
        </li>
    <xsl:for-each select="$currentPage/ancestor-or-self::* [@level=$level]/* [@isDoc and string(umbracoNaviHide) != '1']">
        <li>
             <xsl:if test="@id = $currentPage/@id">
                <xsl:attribute name="class">selected</xsl:attribute>
             </xsl:if>
            <a href="{umbraco.library:NiceUrl(@id)}">
                <xsl:value-of select="@nodeName"/>
            </a>
        </li>
    </xsl:for-each>
    </ul>

    </xsl:template>
    </xsl:stylesheet>

    I hope this is what you were looking for. Remember to change the homeNode variable so it match your Homepage documenttype alias.

    <!-- Homepage -->
    <xsl:variable name="homeNode" select="$rootNode/Frontpage[@isDoc]" />

    /Dennis

  • Aly Sebastien 19 posts 36 karma points
    Jan 01, 2014 @ 19:25
    Aly Sebastien
    0

    Thanks Dennis. However, it wont let me save the file as it throws up the following errors. Any ideas?

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jan 01, 2014 @ 21:10
    Dennis Aaen
    0

    Hi Aly,

    The reason why you are getting this error message is because you need to change the homeNode variable so it match your Homepage documenttype alias

    In my case it´s Forside (The document type for the frontpage), I have made an screen shot so you can see what you should change:

    <!-- Homepage -->
    <xsl:variable name="homeNode" select="$rootNode/Frontpage[@isDoc]"/>

    I hope this helps you.

    /Dennis

  • Aly Sebastien 19 posts 36 karma points
    Jan 02, 2014 @ 21:46
    Aly Sebastien
    0

    Yeh, it worked. Thank you very much. However, I'm told that XSLT is to be avoided and have been advised to use the following: However, this too now throws up errors?

    var homePage = Model.AncestorOrSelf(1);
        <ul>
            <li class="@(homePage.Id == Model.Id ? "active" : null)">
                <a href="@homePage.Url">@homePage.Name</a>
            </li>
            @foreach (var page in homePage.Children.Where("Visible"))
            {
                <li class="@(page.Id == Model.Id ? "active" : null)">
                    <a href="@page.Url">@page.Name</a>
                </li>
            }
        </ul>

     

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jan 02, 2014 @ 22:07
    Dennis Aaen
    0

    Hi Aly,

    If you want to do it in Razor, this code should do it for you.

    @inherits umbraco.MacroEngines.DynamicNodeContext

    @*
        Macro to display child pages below the root page of a standard website.
        Also highlights the current active page/section in the navigation with
        the css class "current".
    *@

    @{
        @*Get the root of the website *@
        var root = Model.AncestorOrSelf(1);

    }
    <ul>
        @{ var homeActive = ""; }

        @if(root.Id == Model.Id){
            homeActive = "current";
        }
        <li class="@homeActive">
            <a href="@root.Url">
                @root.Name             
            </a>
        </li>

        @foreach (var page in root.Children.Where("Visible"))
        {
            <li class="@page.IsAncestorOrSelf(Model, "current", "")">
                <a href="@page.Url">@page.Name</a>
            </li>
        }
    </ul>

    /Dennis

  • Aly Sebastien 19 posts 36 karma points
    Jan 02, 2014 @ 23:28
    Aly Sebastien
    0

    Great news. That also worked. Now I have a re-usable chunk of code to use for a basic navigation. Thanks very much, You're a star !

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jan 02, 2014 @ 23:44
    Dennis Aaen
    0

    Aly you are very welcome. I´m glad that I could help you, 

    I think you should mark this topic as solved so other can see what worked for you. Since you are new to the Umbraco forum, I assume that you don´t know how to do this. Therefore, I would of course guide you how to do it. Find the person that gives you the correct answer and then click the green tick.

    Hope the explanation makes sense.

    /Dennis

  • 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.

    Continue discussion

Please Sign in or register to post replies