Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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

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

@zhoudelun

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

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

@iamspix

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
Something went wrong with that request. Please try again.