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 8964483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:36:02+00:00 2026-06-15T16:36:02+00:00

So far mustache is great, but logic-less now does not seem to be a

  • 0

So far mustache is great, but logic-less now does not seem to be a feature. Suppose, I have to render template in which I can have user’s email and name.

If both email and name are present I want my rendered result to be:

<a href="mailto:EMAIL">NAME</a>

If email is present, but not name, the result would be:

<a href="mailto:EMAIL">EMAIL</a>

If just name is present, template should be rendered to:

NAME

So now I have to do something like:

{{#email}}<a href="mailto:{{email}}">{{#name}}{{name}}{{/name}}{{^name}}{{email}}{{/name}}</a>{{/email}}{{^email}}{{name}}{{/email}}

This is SO UGLY! And this is just 3 conditions! What if I will need more? I feel like there should be another way of doing the same in mustache. Or shouldn’t it?

So, mustache developers think that being logic-less is a good thing, but I have an argument. Most of the times we need to check some variable in template (most of the times we just need to check only existence). So I would do something like:

{% if email %}{{ email }}{% else %}{{ name }}{% endif %}

and this looks much more comprehensive then

{{#email}}{{email}}{{/email}}{{^email}}{{name}}{{/email}}

isn’t it?

If I need to check name too:

{% if email %}{{email}}{% elif name %}{{name}}{% else %}Anonymous{% endif %}

vs

{{#email}}{{email}}{{/email}}{{^email}}{{#name}}{{name}}{{/name}}{{^name}}Anonymous{{/name}}{{/email}}

If no, would you suggest another templating library for rendering templates in javascript? Integration with jQuery (jQuery-plugin) is an advantage, but not a requirement – I can do it on my own.

Thanks.

  • 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-15T16:36:04+00:00Added an answer on June 15, 2026 at 4:36 pm

    There’s always Handlebars which is a little more feature rich than vanilla Mustache (it’s a superset of Mustache), and of course you can use any of the other countless templating engines available for JS. If you go with Handlebars you could even reimplement a version of the {{#if}} block helper that accepts multiple arguments, like this (warning: very slightly tested, writing mostly out of the top of my head)

    Handlebars.registerHelper('if', function() {
        var args = Array.prototype.slice.call(arguments),
            options = args.splice(-1, 1)[0],
            allPass = true,
            context;
    
        for (var i = 0; i < args.length; i++) {
            context = args[i];
    
            if(Object.prototype.toString.call(context) === "[object Function]") { 
                context = context.call(this);
            }
    
            if (!context || Handlebars.Utils.isEmpty(context)) {
                allPass = false;
                break;
            }
        }
    
        if(allPass) {
            return options.fn(this);
        } else {
            return options.inverse(this);
        }
    });
    

    and use it like this:

    var template = "{{#if a b c}}pass{{else}}fail{{/if}}";
    var c = Handlebars.compile(template);
    console.log(c({a: true, b: pass, c: true}))
    

    Be aware that this can be considered “cheating” by purists as you are implementing some kind of a compound logic operator, even more making it accept an indeterminate amount of params.
    The point of Mustache is to have a very minimal spec that is easily ported across programming languages so that you can interchangeably reuse your templates in the back end and front end.

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

Sidebar

Related Questions

So far i have the following code, but it doesn't seem to be working,
So far I have not been able to find an answer, but I wanted
So far I've got the following regex that matches against regular domains but not
So far I have this <UserControl.Resource> <LinearGradientBrush x:Key=KeyDownBrush .....> Now I would like to
I have started using mustache.js and so far I am very impressed. Although two
I am trying to understand the concept behind logic-less temlpates, but I'm finding myself
As far as I can tell, clicking on submit button does not take me
So far I have only been updated a view from within its controller. I
As far as I can see this is not the case, however, I thought
As far as I know, C2DM is per app per device thing..but my question

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.