« Mustache » : différence entre les versions
De Banane Atomic
Aller à la navigationAller à la recherche
Aucun résumé des modifications |
|||
Ligne 3 : | Ligne 3 : | ||
* [https://mustache.github.io/#demo Demo] | * [https://mustache.github.io/#demo Demo] | ||
* [https://github.com/janl/mustache.js/ Doc] | * [https://github.com/janl/mustache.js/ Doc] | ||
= Exemple = | |||
<kode lang='html'> | |||
<tbody> | |||
{{#.}} | |||
<tr> | |||
<td>{{id}}</td> | |||
<td>{{fields.title}}</td> | |||
<td>{{fields.state}}</td> | |||
<td><ul>{{#items}}<li>{{title}}</li>{{/pullRequests}}</ul></td> | |||
</tr> | |||
{{/.}} | |||
</tbody> | |||
</kode> | |||
<kode lang='js'> | |||
[ | |||
{ | |||
"id": 1, | |||
"fields": | |||
{ | |||
"title": "T1", | |||
"state": "S1" | |||
}, | |||
"items": | |||
[ | |||
{ "title": "item1" } | |||
] | |||
} | |||
] | |||
</kode> | |||
= [https://github.com/janl/mustache.js/#include-templates Include template]= | = [https://github.com/janl/mustache.js/#include-templates Include template]= |
Version du 25 mars 2021 à 16:51
Links
Exemple
<tbody> {{#.}} <tr> <td>{{id}}</td> <td>{{fields.title}}</td> <td>{{fields.state}}</td> <td><ul>{{#items}}<li>{{title}}</li>{{/pullRequests}}</ul></td> </tr> {{/.}} </tbody> |
[ { "id": 1, "fields": { "title": "T1", "state": "S1" }, "items": [ { "title": "item1" } ] } ] |
Include template
<script id="template" type="x-tmpl-mustache"> Hello {{ name }} </script> <script> $(() => { const template = $("#template").html(); const rendered = Mustache.render(template, { name: 'Nico' }); }); </script> |
Doesn't handle dots in property name
// a solution could be to replace the dots in property names (it uses lodash) function replaceDotWithUnderscore(obj) { _.forOwn(obj, (value, key) => { if (_.includes(key, '.')) { const cleanKey = _.replace(key, /\./g, '_'); obj[cleanKey] = value; delete obj[key]; } // recursive call if the value is an object or an array if (_.isObject(value)) { return replaceDotWithUnderscore(value); } }); return obj; } |