Permalink
Browse files

Add startsWithPathInfo filter to also mark parent links as active

  • Loading branch information...
mythz committed Jan 30, 2019
1 parent 4903b36 commit b932b4697d9864b4cb3c713232b3452b7cccb92a
@@ -13,6 +13,10 @@ public partial class TemplateDefaultFilters
public bool matchesPathInfo(TemplateScopeContext scope, string pathInfo) =>
scope.GetValue("PathInfo")?.ToString().TrimEnd('/') == pathInfo?.TrimEnd('/');

public bool startsWithPathInfo(TemplateScopeContext scope, string pathInfo) => pathInfo == "/"
? matchesPathInfo(scope, pathInfo)
: scope.GetValue("PathInfo")?.ToString().TrimEnd('/').StartsWith(pathInfo?.TrimEnd('/') ?? "") == true;

public object ifMatchesPathInfo(TemplateScopeContext scope, object returnTarget, string pathInfo) =>
matchesPathInfo(scope, pathInfo) ? returnTarget : null;

@@ -1,13 +1,15 @@
{{ { '/': 'Home',
'/about': 'About',
'/services': 'Services',
'/services/acme': 'ACME Services',
'/services/acme/widget': 'ACME Widget Service',
'/contact': 'Contact',
} | toList | assignTo: links }}

<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav ml-auto">
{{#each links}}
<li {{ {active: matchesPathInfo(Key)} | htmlClass }}>
<li {{ {active: startsWithPathInfo(Key)} | htmlClass }}>
<a class="nav-link" href="{{Key}}">{{Value}}</a>
</li>
{{/each}}

0 comments on commit b932b46

Please sign in to comment.