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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:21:16+00:00 2026-06-05T14:21:16+00:00

mustache.js whether to support such an expression? -> {{type == a}} How do I

  • 0

mustache.js whether to support such an expression? -> {{type == “a”}}

How do I do?

Mustache:

{{#items}}
  {{type == "a"}} 
    <li>{{name}} {{type}}</li>
  {{/link}}
  {{type == "b"}} 
    <strong>{{name}} {{type}}</strong>
  {{type == "c"}} 
    <span>{{name}} {{type}}</span>
  {{/link}}
{{/items}}

JSON:

{
  "header": "Colors",
  "items": [
      {"name": "red", "type": "a", "url": "#Red"},
      {"name": "green", "type": "b", "url": "#Green"},
      {"name": "blue", "type": "c", "url": "#Blue"}
  ],
  "empty": false
}

want Output:

<li>red a</li>
<strong>green b</strong>
<span>blue c</span>
  • 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-05T14:21:17+00:00Added an answer on June 5, 2026 at 2:21 pm

    You can’t because Mustache is logic-less.

    Best practice is to change your input JSON in such a way that the ‘logic’ is already included in the input. Something like:

    {
      "header": "Colors",
      "items": [
          {"a": {"name": "red", "url": "#Red"}},
          {"b": {"name": "green", "url": "#Green"}},
          {"c": {"name": "blue", "url": "#Blue"}}
      ],
      "empty": false
    }
    

    Then:

    {{#items}}
      {{#a}}
        <li>{{name}} {{type}}</li>
      {{/a}}
      {{#b}}
        <strong>{{name}} {{type}}</strong>
      {{/b}}
      {{#c}}
        <span>{{name}} {{type}}</span>
      {{/c}}
    {{/items}}
    

    Would produce what you wanted.

    EDIT
    Somehow I couldn’t get the above example to work. I’m using Hogan (a version of mustache maintained by Twitter which is faster as well as having a couple of extra things you can do with it, be sure to check it out) but I’m pretty sure that hasn’t got anything to do with the fact that the above stuff should just work.

    Anyway, I could get the following to work on the supplied demo page, which stays closer with the original example, and accomplishes the output you wanted. General note: You can check for existence of a field (e.g using {{#isa}}) but you can’t have any other logic in the tags.

    {{#items}}
      {{#isa}}
       <li><strong>{{name}}</strong></li>
      {{/isa}}
      {{#isb}}
       <li><a href="{{url}}">{{name}}</a></li>
      {{/isb}}
      {{#isc}}
        <li><span href="{{url}}">{{name}}</span ></li>
      {{/isc}}
    {{/items}}
    

    JSON

    {
      "header": "Colors",
      "items": [
          {"name": "red", "isa": true, "url": "#Red"},
          {"name": "green", "isb": true, "url": "#Green"},
          {"name": "blue", "isc": true, "url": "#Blue"}
      ],
      "empty": false
    }
    

    hth

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

Sidebar

Related Questions

I have a mustache.js template defined like so: <script id=wishlist_template type=text/html> <h1>aaa{{ title }}</h1>
I am using JSON and MUSTACHE, for templates in my new site. But I
I'm attempting to use mustache.js as a templating system for JSON data that I'm
I'm using mustache. I'm generating a list of notifications. A notification JSON object looks
In mustache , you iterate like this: {{#items}} <div>The item {{item}}</div> {{/items}} In handlebars.js
In Mustache can I print out the name of an associative array key instead
I have a CouchDB with following doc: { name:Chris, tags:[mustache, music, couchdb] } {
Currently I'm using Mustache templates ( http://mustache.github.com/ ) both on the client and server-side,
Does the Ruby Mustache API have a way to return the key names from
I'm using mustache.js to render a template in javascript. I'd like to check if

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.