Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a nav-bar in logic-less template engine like mustache

I am trying to understand the concept behind logic-less temlpates, but I'm finding myself hitting a wall.

I want to implement a simple navigation bar, e.g. "Home, About, Contact" links at the top of every page, and the "current" one should be highlighted with a different class (I'm using bootstrap). But how can I do this in a sensible fashion? So far I have:

  1. Move the nav to every template, and copy the whole thing (not DRY, ugly).
  2. Use keys instead of values, i.e. render('home', { on_home_page: true }); with <a href="/" {{#on_home_page}}class="active"{{/on_home_page}}>Home</a>. This is better, but still annoying that I have to create N variables to hold 1-variable worth of data.
  3. create the nav in the controller, i.e. pass in { 'Home': {link: '/', active: false}, 'About: {link: '/about', active: true} } or similar. I dislike this because it has the opposite problem of logic-less templates. Now I have HTML-ful controllers...

Given the above options, I like (2) the best. But what I would prefer is some way to have a single variable to check, like:

// controller
render('about', {active: 'about'});
render('home', {active: 'home'});

// mustache nav
<a href="/" {{#if active == 'home'}}class="active"{{/if}}>Home</a>
<a href="/about" {{#if active == 'about'}}class="active"{{/if}}>About</a>

I'm sure this comes up all the time for mustache experts --- what's the best way to deal with it?

like image 421
Robert Martin Avatar asked May 08 '12 16:05

Robert Martin


People also ask

What is the best way to create a navigation bar?

A navigation bar needs standard HTML as a base. In our examples we will build the navigation bar from a standard HTML list. A navigation bar is basically a list of links, so using the <ul> and <li> elements makes perfect sense:

Does a navigation bar need list markers?

A navigation bar does not need list markers The code in the example above is the standard code used in both vertical, and horizontal navigation bars, which you will learn more about in the next chapters.

How to make a simple navbar look beautiful?

A simple navbar shouldn’t be boring at all. You should stick to a pre-decided brand color to make the design more consistent. You can experiment with numerous color schemes and use lighter or darker shades for highlighting and dropdown menus.

How to transform boring HTML menus into good-looking navigation bars?

With CSS you can transform boring HTML menus into good-looking navigation bars. A navigation bar needs standard HTML as a base. In our examples we will build the navigation bar from a standard HTML list.


1 Answers

There's no way to deal with this using vanilla Mustache. You have two options that let your JSON data and template stay clean:

1- using Mustache, write a helper function so that you can use it like this:

var selected_item = function(param) {
    if (param == this.active) {
        return 'active';
    }
};

(disclaimer: I wrote this helper out of the top of my head, might not work as-is but I think you get the point)

<a href="/" class="{{#selected_item}}home{{/selected_item}}">Home</a>
<a href="/about" class="{{#selected_item}}about{{/selected_item}}">About</a>

then mix-in that helper into your JSON data, best way probably being overloading Mustache.render so that every call to render adds your helper into the mix. Notice that leaving the class="" part outside of the helper allows you to have multiple different classes on each menu item while still having your "logic" for the active part.

2- switch to Handlebars which allows for this kind of basic logic. Handlebars is a superset of Mustache, stuff that works on vanilla Mustache will directly work in Handlebars so upgrading is easy. Be careful though, that once you have upgraded and modified your templates to work with Handlebars there's no turning back.

like image 175
gonchuki Avatar answered Oct 16 '22 21:10

gonchuki