Created
February 12, 2016 06:35
-
-
Save darkcolonist/85f2d2212e88edf5fcb8 to your computer and use it in GitHub Desktop.
simple usage of mustache.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>mustache says hi!</title> | |
</head> | |
<body> | |
<table id="students"> | |
<thead> | |
<tr> | |
<th>ID</th> | |
<th>Name</th> | |
<th>Gender</th> | |
</tr> | |
</thead> | |
<tbody></tbody> | |
</table> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/mustache.js/2.2.1/mustache.min.js"></script> | |
<script id="studentList" type="text/html"> | |
{{#students}} | |
<tr> | |
<td>{{id}}</td> | |
<td>{{name}}</td> | |
<td>{{gender}}</td> | |
</tr> | |
{{/students}} | |
</script> | |
<script type="text/javascript"> | |
function repopulate(){ | |
var students = [ | |
{ | |
id: "1", | |
name: "cris", | |
gender: "male", | |
}, | |
{ | |
id: "2", | |
name: "zelle", | |
gender: "female", | |
}, | |
{ | |
id: "3", | |
name: "donnie", | |
gender: "male", | |
}, | |
] | |
var template = document.getElementById("studentList").innerHTML | |
var mTemplate = Mustache.render(template, { | |
students: students | |
}) | |
$("#students").append(mTemplate) | |
} | |
$(document).ready(function(){ | |
repopulate() | |
}) | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment