« Mustache » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
Ligne 7 : Ligne 7 :
<kode lang='html'>
<kode lang='html'>
<script id="template" type="x-tmpl-mustache">
<script id="template" type="x-tmpl-mustache">
Hello {{ name }}!
Hello {{ name }}
</script>
</script>



Version du 29 janvier 2021 à 22:07

Links

Include template

Html.svg
<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

Js.svg
// 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;
}