public
Last active — forked from ebaxt/radio_mustach_example.html

Checking radio with Mustache

  • Download Gist
radio_mustach_example.html
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
<!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>

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.

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.