Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixing active main menu item highlighting.

  • Loading branch information...
commit 1fb3199833d0f1faf6eb8228f6db4ff02e2484dd 1 parent 23d5d86
@augustl augustl authored
Showing with 17 additions and 6 deletions.
  1. +3 −3 layout.html
  2. +14 −3 server.js
View
6 layout.html
@@ -11,9 +11,9 @@
<div class="centered_content">
<a href="/" id="logo_image"></a>
<ul id="main_menu">
- <li><a href="/" class="active">Home</a></li>
- <li><a href="/docs/">Documentation</a></li>
- <li><a href="/community/">Community</a></li>
+ <% menu.forEach(function (m) { %>
+ <li><a href="<%= m.href %>" class="<%= m.className || "" %>"><%= m.text %></a></li>
+ <% }) %>
</ul>
</div>
</div>
View
17 server.js
@@ -21,7 +21,7 @@ function serveTemplate(path, pathname, res) {
if (pathname.slice(pathname.length - 1) == "/") {
res.writeHead(200, {"Content-Type": "text/html"});
fs.readFile(path, function (err, data) {
- res.write(renderTemplate(data.toString("utf8")));
+ res.write(renderTemplate(pathname, data.toString("utf8")));
res.end();
});
} else {
@@ -30,8 +30,19 @@ function serveTemplate(path, pathname, res) {
}
}
-function renderTemplate(content) {
- return ejs.render(LAYOUT, {content: content});
+function renderTemplate(pathname, content) {
+ var menu = [
+ {href: "/", text: "Home"},
+ {href: "/docs/", text: "Documentation", matcher: /^\/docs/},
+ {href: "/community/", text: "Community"}
+ ];
+
+ menu.forEach(function (item) {
+ var current = item.matcher ? item.matcher.test(pathname) : pathname == item.href;
+ if (current) item.className = "active";
+ });
+
+ return ejs.render(LAYOUT, {content: content, menu: menu});
}
function notFound(res) {
Please sign in to comment.
Something went wrong with that request. Please try again.