Skip to content

Instantly share code, notes, and snippets.

@darkcolonist
Created February 12, 2016 06:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darkcolonist/85f2d2212e88edf5fcb8 to your computer and use it in GitHub Desktop.
Save darkcolonist/85f2d2212e88edf5fcb8 to your computer and use it in GitHub Desktop.
simple usage of mustache.js
<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