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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:00:01+00:00 2026-06-16T00:00:01+00:00

Is there a way to make a switch statement in Mustache.js I have a

  • 0

Is there a way to make a switch statement in Mustache.js

I have a variable which can be one of the four different values.
Depends on its value I want to output slightly different template.

Is there a way to do this?

P.S I have seen this question, but nevertheless the question sounds the same, but in the answer the person was answering how to achieve if/else functionality, which I can find here.

Explanation about the code:

I have a list of templates

var templates = {
    friends :
        'here there is some html with {{vars}}'+
        'and a button on which I have to write something like'+
        'add to friends | already your friend | invitation was sent | your invitation was rejected'
    }
}

So basically I am doing an ajax call and together with vars it returns me type, which can be one of the following things 0,1,2,3. Depending on this, I have to write specific text

  • 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-16T00:00:03+00:00Added an answer on June 16, 2026 at 12:00 am

    Edit: I’m assuming you’re using javascript. After seeing your use case you can resolve this by passing a function to mustache, for example:

    function getType(){
        switch(viewObject.typeid){
        //your cases here return what you want the function to print
        }
    }
    

    now, the object you send to mustache should have typeid, this will allow you to move the logic from Mustache (which is again, logicless) back to javascript. Then you can place {{{getType}}} in your code.

    Original:

    If I may ask, what is the specific usage you need your switch/case statement for? There may be a simpler solution using Mustache. I’ve written a lot of Mustache code and never ran into a “switch-case” scenario that was not better solved with passing a function with the view-model

    Remember that you can pass a function as a parameter to Mustache, this will allow you to move the logic from Mustache back to the view-model which is (most of the times) a good thing.

    If that is not the case you can always switch the handlebars:

    Mustache is used for logicless templating. If you find out that you have too much logic in your templating, Mustache is probably not the correct choice for you, you can use Handlebars instead which extends Mustache, any current mustache code you have will continue to work.

    http://handlebarsjs.com/

    Although handlebars does not contain switch-case statements, it allows you to write if/else statements in a much simpler manner, and it will allow you to define helper functions which can accomplish your functionality.

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

Sidebar

Related Questions

Is there any way to make a case condition in a switch statement where
What is the best way to reuse switch logic. I have this switch statement
Is there a way to make a button's background color change from a color
Is there a way to make MySQL cause some kind of error when doing
Is there a way to make Timeline start recording EXACTLY at the time where
Is there a way to make the SVG version of an image not being
Is there a way to make each form, (all controls, all strings, all integers
Is there a way to make my mysql and php server automatically display dates
Is there a way to make a macro to save a file with the
Is there a way to make my js files not viewable or debuggable through

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.