Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle an IF STATEMENT in a Mustache template?

I'm using mustache. I'm generating a list of notifications. A notification JSON object looks like:

[{"id":1364,"read":true,"author_id":30,"author_name":"Mr A","author_photo":"image.jpg","story":"wants to connect","notified_type":"Friendship","action":"create"}] 

With mustache, how can I do a if statement or case statement based on the notified_type & action...

If notified_type == "Friendship" render ......

If notified_type == "Other && action == "invite" render.....

How does that work?

like image 327
AnApprentice Avatar asked Dec 18 '11 00:12

AnApprentice


People also ask

How do you escape a mustache?

Mustache does escape all values when using the standard double Mustache syntax. Characters which will be escaped: & \ " < > (as well as ' in Ruby >= 2.0 ). To disable escaping, simply use triple mustaches like {{{unescaped_variable}}} .

What is mustache syntax?

Mustache is a logic-less template syntax. It can be used for HTML, config files, source code - anything. It works by expanding tags in a template using values provided in a hash or object. We call it "logic-less" because there are no if statements, else clauses, or for loops.

What are .mustache files?

Mustache is a logic-less templating system. It permits you to use pre-written text files with placeholders that will be replaced at run-time with values particular to a given request. For more general information on Mustache, consult the mustache specification.

How handlebars JS is different from mustache JS?

Handlebars. js is an extension to the Mustache templating language created by Chris Wanstrath. Handlebars. js and Mustache are both logicless templating languages that keep the view and the code separated like we all know they should be; Mustache: Logic-less templates.


1 Answers

Just took a look over the mustache docs and they support "inverted sections" in which they state

they (inverted sections) will be rendered if the key doesn't exist, is false, or is an empty list

http://mustache.github.io/mustache.5.html#Inverted-Sections

{{#value}}   value is true {{/value}} {{^value}}   value is false {{/value}} 
like image 85
Lucas Avatar answered Oct 05 '22 02:10

Lucas