How do I reference a field name that contains a dot in mustache template?

后端 未结 1 636
北海茫月
北海茫月 2021-01-12 14:52

How do I reference a field name that contains a dot in mustache template? For instance, if I have a view like

{
  \"foo.bar\": \"my value\"
}
相关标签:
1条回答
  • 2021-01-12 15:50

    You can't read a key with a . in it from Mustache. The Mustache spec dictates that . is used to split content names. Mustache provides a means of escaping but only for HTML content.

    Mustache spec: interpolation

    You will need to pre-process your data to make it usable in a Mustache template. How you do this will depend on how widespread the issue is.

    I found a simple example to remap a property in JavaScript, written by Jon:

    function rename(obj, oldName, newName) {
        if(!obj.hasOwnProperty(oldName)) {
            return false;
        }
    
        obj[newName] = obj[oldName];
        delete obj[oldName];
        return true;
    }
    

    Source: Rename the keys… in an object

    0 讨论(0)
提交回复
热议问题