<!DOCTYPE html>
<head>
<script src="http://cdnjs.cloudflare.com/ajax/libs/mustache.js/0.3.0/mustache.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
$(function() {
var tpl = $('#fam').html();
data = {
"families" : [
{
"surname": "Jones",
"members": [
{"given": "Jim"},
{"given": "John"},
{"given": "Jill"}
]
},
{
"surname": "Smith",
"members": [
{"given": "Steve"},
{"given": "Sally"}
]
}
]
},
html = Mustache.to_html(tpl, data);
$("#main").append(html);
});
</script>
</head>
<div id="main"></div>
<script type="template/text" id="fam">
<ul>
{{#families}}
<li>{{surname}}
<ul>
{{#members}}
<li>{{given}}</li>
{{/members}}
</ul>
</li>
{{/families}}
</ul>
</script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<ul>
<li v-for="family in families">
{{ family.surname }}
<ul>
<li v-for="member in family.members">
{{ member.given }}
</li>
</ul>
</li>
</ul>
</div>
<script src="https://unpkg.com/vue@next"></script>
<script>
const App = {
data() {
return {
"families": [
{
"surname": "Jones",
"members": [
{ "given": "Jim" },
{ "given": "John" },
{ "given": "Jill" }
]
},
{
"surname": "Smith",
"members": [
{ "given": "Steve" },
{ "given": "Sally" }
]
}
]
}
}
}
Vue.createApp(App).mount('#app')
</script>
</body>
</html>
<htmL>
<head>
(function() {
/***************************/
/** Classe de gestion **/
/** de template JSON: **/
/** templateJSON **/
/***************************/
/** Méthodes : **/
/***************************/
/** 1) Traduction **/
/** du template html **/
/** vers le format JSON **/
/** html(id_template), **/
/***************************/
/** 2) Génération **/
/** du DOM à partir **/
/** du JSON et des data **/
/** to_dom(tpl, data) **/
/***************************/
/** 3) Identificateur **/
/** id(html_id) **/
/***************************/
/** 4) Ajout du html **/
/** à l'identificateur. **/
/** append(html) **/
/***************************/
var tplJSON = new templateJSON();
/*****************************/
/** #id_template = ID du template **/
/** Cette fonction **/
/** génère le format **/
/** JSON,qui sera mappé **/
/** et rempli par les data **/
/*****************************/
var tpl = tplJSON.html('#id_template');
data = {
"families" : [
{
"surname": "Jones",
"members": [
{"given": "Jim"},
{"given": "John"},
{"given": "Jill"}
]
},
{
"surname": "Smith",
"members": [
{"given": "Steve"},
{"given": "Sally"}
]
}
]
},
/***************************/
/** Cette fonction **/
/** génère le DOM **/
/** à partir du JSON **/
/** et des data. **/
/***************************/
html = tplJSON.to_dom(tpl, data);
/************************/
/** #html_id = ID du HTML **/
/************************/
html = tplJSON.id('html_id').append(html);
});
</script>
</head>
<div id="html_id"></div>
<script type="template/text" id="id_template">
<ul>
{{#families}}
<li>{{surname}}
<ul>
{{#members}}
<li>{{given}}</li>
{{/members}}
</ul>
</li>
{{/families}}
</ul>
</script>