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

◇◆丶佛笑我妖孽 提交于 2020-01-11 05:39:05

问题


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"
}

then how can I put my value into a template? Using {{foo.bar}} doesn't work because mustache thinks the dot is part of the path, like there should be a "foo" that has a "bar".


回答1:


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



来源:https://stackoverflow.com/questions/14901683/how-do-i-reference-a-field-name-that-contains-a-dot-in-mustache-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!