Szmyd.Orchard.Modules.Menu 1.1.0 to add class="current" to all levels of menu

Topics: Customizing Orchard, Writing modules
Apr 6, 2011 at 10:36 AM

 


To add class="current" to all levels of menu (css guys will understand why ;))
overwrite C:\inetpub\wwwroot\inspra\src\Orchard.Web\Modules\Szmyd.Orchard.Modules.Menu\Views\MenuItem.cshtml
with this

@{
    // THIS SHAPE OVERRIDES THE DEFAULT SHAPE FOR MENU ITEM FOUND UNDER: Orchard.Core/Shapes/Views/MenuItem.cshtml
    
    // odd formatting in this file is to cause more attractive results in the output.
    var items = (IEnumerable<dynamic>)Enumerable.Cast<dynamic>(Model);
}
@{
if (!HasText(Model.Text)) {
    @DisplayChildren(Model)
} else {
//    if (Model.Href.TrimEnd('/').ToUpperInvariant() == Request.Path.TrimEnd('/').ToUpperInvariant()) {
    List<string> urlparts = new List<string>();
    string fullpath = Request.Path.TrimEnd('/').ToUpperInvariant();

    while (fullpath.Contains("/"))
    {
        urlparts.Add(fullpath);
       fullpath = fullpath.Substring(0, fullpath.LastIndexOf('/'));
    }
    if (urlparts.Contains(Model.Href.TrimEnd('/').ToUpperInvariant()))
    {
    Model.Classes.Add("current");
    }
    var tag = Tag(Model, "li");
    @tag.StartElement
    <a href="@Model.Href">@Model.Text</a>
    if (items.Any()) {
        <ul>
        @DisplayChildren(Model)
        </ul>
    }
    @tag.EndElement
}
}

enjoy.

 

Apr 6, 2011 at 10:53 AM

Hmm... Szmyd's module already did that for me without modification. Are you sure you're not just missing a setting?

Apr 6, 2011 at 11:00 AM

could you point path to that feature in Admin panel please?

I can't find it.

Apr 6, 2011 at 11:42 AM

Ok, can't see the feature anywhere ... for me it just was just working ... but yeah I see now there was a bug in the original code and it only worked because I had child menus with the same URL as the parent.

Either way, the new version (Advanced menu 1.2.1) has this issue fixed and a lot of extra features, but unfortunately requires Orchard 1.1.

Developer
Jun 11, 2011 at 5:34 PM

It was fixed, but introduced some new bugs. Everything will be fixed in upcoming 1.3 release.

Developer
Jun 13, 2011 at 3:34 PM

It's been fixed in the latest release (1.3). There was a bug due to which "current" class was applied only to items sharing part of the URL with the currently viewed page. You can find full release notes here.

I added two CSS classes - "current", which is applied only to the current item, and "selected", which is applied to all items on the selection path up to the top (starting from current).