Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Checking radio with Mustache
<!DOCTYPE html>
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="js/mustache.js"></script>
<div id="formWrapper">
<script id="tmpl" type="x-template">
<label><input type="radio" name="gender" value="male"{{#maleIsDefault}} checked{{/maleIsDefault}}> Male</label>
<label><input type="radio" name="gender" value="female"{{#femaleIsDefault}} checked{{/femaleIsDefault}}> Female</label>
<script type="text/javascript">
var data = {
gender: 'female',
maleIsDefault: function() {
return this.gender == 'male';
femaleIsDefault: function() {
return this.gender == 'female';
var template = $("#tmpl").html();
var result = Mustache.render(template, data);

ebaxt commented Mar 8, 2012

Thanks, less of a hack than my first attempt :)

var view = {
title: "Joe",
calc: function () {
return 2 + 4;

var output = Mustache.render("{{title}} spends {{calc}}", view);

why it only result as 'spends '?
who can help me...

I found that your code is in .html,but mine is in .mustache file.

iamspix commented Oct 20, 2012

can't understand why is this even working. what i did is something similar to this :
if output.gender == 'male' do maleIsDefault, else do femaleIsDefault, which will then assign values to male or female is default, anything between the opening and closing tag will be enabled..

but i want to know how this works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment