Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bobthecow/1978729 to your computer and use it in GitHub Desktop.
Save bobthecow/1978729 to your computer and use it in GitHub Desktop.
Checking radio with Mustache
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="js/mustache.js"></script>
</head>
<body>
<div id="formWrapper">
</div>
<script id="tmpl" type="x-template">
<form>
<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>
</form>
</script>
<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);
$("#formWrapper").html(result);
</script>
</body>
</html>
@ebaxt
Copy link

ebaxt commented Mar 8, 2012

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

@zhoudelun
Copy link

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...

@zhoudelun
Copy link

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

@iamspix
Copy link

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