« 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]
= Template =
<kode lang='html'>
<script type="x-tmpl-mustache" id="template">
    <table>
        <thead>
            <tr>
                <th>Id</th>
                <th>State</th>
                <th>Assigned to</th>
            </tr>
        </thead>
        <tbody>
            {{#.}}
            <tr>
                <td><a href="https://dev.azure.com/msc-dev/Pricing/_workitems/edit/{{id}}">{{id}}</a></td>
                <td>{{fields.System_Title}}</td>
                <td>{{fields.System_State}}</td>
                <td>{{fields.System_AssignedTo.displayName}}</td>
            </tr>
            {{/.}}
        </tbody>
    </table>
</script>
<script>
$(() => {
    const template = $("#template").html();
    const rendered = Mustache.render(template, json);
});
</script>
</kode>


= [https://github.com/janl/mustache.js/issues/343 Doesn't handle dots in property name] =
= [https://github.com/janl/mustache.js/issues/343 Doesn't handle dots in property name] =

Version du 28 janvier 2021 à 17:25

Links

Template

Html.svg
<script type="x-tmpl-mustache" id="template">
    <table>
        <thead>
            <tr>
                <th>Id</th>
                <th>State</th>
                <th>Assigned to</th>
            </tr>
        </thead>
        <tbody>
            {{#.}}
            <tr>
                <td><a href="https://dev.azure.com/msc-dev/Pricing/_workitems/edit/{{id}}">{{id}}</a></td>
                <td>{{fields.System_Title}}</td>
                <td>{{fields.System_State}}</td>
                <td>{{fields.System_AssignedTo.displayName}}</td>
            </tr>
            {{/.}}
        </tbody>
    </table>
</script>
<script>
$(() => {
    const template = $("#template").html();
    const rendered = Mustache.render(template, json);
});
</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;
}