Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7927491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:19:46+00:00 2026-06-03T19:19:46+00:00

I am trying to understand the concept behind logic-less temlpates, but I’m finding myself

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T19:19:47+00:00Added an answer on June 3, 2026 at 7:19 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to understand the concepts behind DDD, but I find it hard to
I'm trying to understand this concept but I don't get it yet. I'm new
I am trying to understand programming logic behind the beans of Spring. In examples
I'm trying to understand the concept of monads and I want to know if
I'm trying to comprehend the concept behind MVC and URL routing. I understand that
The question might sound vague but I'm trying to understand the general concept of
So I have been trying to understand the concept of 3D picking but as
I am trying to understand the concept of currying and calling a function which
I am trying to understand the concept of timestamps in request headers in web
I'm a beginner in C and I'm trying to understand the concept of pointer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.