问题描述
如何在 mustache 模板中引用包含点的字段名称?例如,如果我有一个像
这样的视图{"foo.bar": "我的价值"}
那我怎样才能把my value
放到模板中呢?使用 {{foo.bar}}
不起作用,因为 mustache 认为点是路径的一部分,就像应该有一个带有bar"的foo".
您无法从 Mustache 读取带有 .
的密钥.Mustache 规范规定 .
用于拆分内容名称.Mustache 提供了一种转义方法,但仅适用于 HTML 内容.
Mustache 规范:插值
您需要对数据进行预处理,使其可用于 Mustache 模板.您如何做到这一点取决于问题的普遍程度.
我找到了一个简单的例子来重新映射 JavaScript 中的属性,由 Jon 编写:
function rename(obj, oldName, newName) {if(!obj.hasOwnProperty(oldName)) {返回假;}对象[新名称] = 对象[旧名称];删除对象[旧名称];返回真;}
来源:重命名对象中的键……>
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".
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
这篇关于如何在 mustache 模板中引用包含点的字段名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!