mustache

Mustache javascript: how to handle with boolean values

非 Y 不嫁゛ 提交于 2019-12-05 11:58:16
问题 I have a javascript object obj and the value of the key can be true or false . This value is passed to mustache template. // javascript object obj = { like: true // or false } // template <span> {{ like }} </span> Now I would like having the result of the rendering in this way: <span> Like <!-- If {like: true} ---> </span> <span> Unlike <!-- If {like: false} ---> </span> What is the best way to make it in mustache template? 回答1: it's just like this: <span> {{#like}} Like <!-- If {like: true}

How to set the selected item in a radio button group in handlebars template?

微笑、不失礼 提交于 2019-12-05 11:21:23
In a handlebars template, how do you set a radio button group to the right value using only the template? Can this be done directly in the template? For an example, let's say there's a radio button group like this: <label><input type="radio" name="mode" value="auto">Auto</label><br> <label><input type="radio" name="mode" value="on">On</label><br> <label><input type="radio" name="mode" value="off">Off</label><br> The data coming into the template has a value for mode: {mode: "on"} I want to end up with this after template expansion: <input type="radio" name="mode" value="auto">Auto<br> <input

Render simple array using Mustache.Js

懵懂的女人 提交于 2019-12-05 09:20:21
问题 Having a array like below var arrNames = ["Stackoverflow","StackExchange","Webmaster","Programmers"]; how should a template look for working with mustache.js javascript template. I tried below but no clues {{#}}{{key}}{{/}} 回答1: From the documentation: When looping over an array of strings, a . can be used to refer to the current item in the list. Template: {{#musketeers}} * {{.}} {{/musketeers}} View: { "musketeers": ["Athos", "Aramis", "Porthos", "D'Artagnan"] } Output: Athos Aramis Porthos

backbone toJSON with helper methods

前提是你 提交于 2019-12-05 06:48:22
问题 I have a backbone model with attributes and some helper methods that output something other than the actual attribute (for formatting for example). However, when I call toJSON , only the attributes are returned, so my mustache templates can't access those helper methods. Is there any way to resolve this? Or is there a different approach I should take? Is the only way around this to create a formatted version of the attribute and update it each time that attribute changes? 回答1: Jorge, i would

how to have grunt task render mustache partials to static HTML

十年热恋 提交于 2019-12-05 06:00:16
问题 Background I've been using grunt.js with a hogan.js task to build the static HTML for our internal docs. I'm learning JavaScript as I go, but I've gotten the task to work well enough for layouts and pages, but it would really help our workflow to have the hogan task render mustache partials to HTML, as in the example in this gist: https://gist.github.com/4132781 Current Setup and what I want to accomplish All of our mustache partials are in a folder called "partials". Ideally when the grunt

Mustache js takes parent's object scope when none is found in the current one

你说的曾经没有我的故事 提交于 2019-12-05 05:35:05
According to the mustache RFC A {{name}} tag in a basic template will try to find the name key in the current context. If there is no name key, nothing will be rendered. I therefore expected this: var template = '{{#anArray}}{{aString}}{{/anArray}}'; var json = { "aString":"ABC", "anArray": [1,{"aString":"DEF"}] }; To give me once rendered: "DEF" However mustache.js looks for values in the parent's scope. Which gives me "ABCDEF" Do the context actually means including all the parents scopes ? http://jsfiddle.net/ZG4zd/20/ Short answer: yes. A bit longer answer. Context.prototype.lookup does a

Why should we wrap our templates inside script blocks?

只愿长相守 提交于 2019-12-05 05:01:21
Background All the JS template engines recommend putting your template text inside script blocks like so: <script id="peopleTemplate" type="text/template"> {#people} <div class="person">Name: {firstName} {lastName}</div> {/people} </script> but many developers (understandably) dislike this because they lose HTML syntax highlighting in their code editor inside the script block. I've seen the workarounds like this: Keep correct HTML syntax highlighting in <script> "text/html" templates . This question is not asking about workarounds. I know one danger is that web browsers will attempt to fix

Mustache (or Handlebars) iterating over two lists

落花浮王杯 提交于 2019-12-05 04:52:21
I have two arrays: var content = { "girls": ["Maria", "Angela", "Bianca"], "digits": ["21.143.191.2", "123.456.78.90", "971.6.17.18.1"] }; and a template: <script id="template" type="text/template"> <ul> <li><a href="{{digits}}">{{girls}}</a></li> </ul> </script> I'd like the end result to be: <ul> <li><a href="21.143.191.2">Maria</a></li> <li><a href="123.456.78.90">Angela</a></li> <li><a href="971.6.17.18.1">Bianca</a></li> </ul> I've tried block mustaches like {{#girls}} {{.}} {{/girls}} and {{#digits}} {{.}} {{/digits}} but no matter which way I nest them I seem to get repeats instead of

How to run a callback when Mustache.js has finished rendering template

折月煮酒 提交于 2019-12-05 03:18:54
Is there a clean way to define/run a callback function once Mustache.js has finished rendering a template and inserting it into the DOM? For example, something like this: Mustache.render(template, viewModel, function() {...}); The best I've been able to come up with is counting the number of nodes in my view model that will be inserted into the DOM, and then using setInterval to check if that many nodes exist in the DOM. Once they do, I can then call the function I want. This seems inefficient and potentially buggy to me, but I don't know what else to do. Not related to mustache, actually it

Rails Client side / Server side rendering using single template (handlebars or Mustache) with Sammy.js

心已入冬 提交于 2019-12-05 02:20:45
问题 I've searched the web for a while looking for a tutorial, but haven't had much luck. From what I understand, Twitter is using a single Mustache.js template in rails to render from the server on first page load, and then through their own ajax transition system (much like sammy.js). I can get handlebars and sammy.js loaded in rails, but I can't figure out how to share a single template file from server(rails) & client(sammy) side. 回答1: I have not personally built anything where I've used the