检查键的值是否是带有 Handlebars 的字符串
Check if value of keys is a string with Handlebars
我有一个这样的对象:
{
id: 1,
type1: 'Adjective',
type2: 'Noun',
type3: 'Noun',
type4: 'Noun',
type5: 'Plural Noun',
type6: 'Person',
type7: 'Place',
type8: 'Job',
type9: 'Adjective',
type10: 'Adjective',
type11: 'Famous Person',
type12: 'Noun',
type13: 'Noun',
type14: '',
createdAt: 2019-10-11T06:49:38.000Z,
updatedAt: 2019-10-11T06:49:38.000Z
}
有什么方法可以使用 handlebars helper 获取值为字符串的键吗?
您可以编写 handlebars 配置助手来检查这个
module.exports.hbs = exphbs.create({
defaultLayout: 'layout',
helpers: {
//You can Name this anything
checkIfValuesOfJsonAreStrings: function(jsonList, context) {
for (var key in obj) {
if(typeOf obj[key] == "String"){
}else{
return context.inverse(this); // This False
}
}
return context.fn(this)
}
})
现在,在编写 .hbs 代码时,您可以包含
{{# checkIfValuesOfJsonAreStrings}}
<div>Yah all the values in JSON are string</div>
{{/checkIfValuesOfJsonAreStrings}}
我有一个这样的对象:
{
id: 1,
type1: 'Adjective',
type2: 'Noun',
type3: 'Noun',
type4: 'Noun',
type5: 'Plural Noun',
type6: 'Person',
type7: 'Place',
type8: 'Job',
type9: 'Adjective',
type10: 'Adjective',
type11: 'Famous Person',
type12: 'Noun',
type13: 'Noun',
type14: '',
createdAt: 2019-10-11T06:49:38.000Z,
updatedAt: 2019-10-11T06:49:38.000Z
}
有什么方法可以使用 handlebars helper 获取值为字符串的键吗?
您可以编写 handlebars 配置助手来检查这个
module.exports.hbs = exphbs.create({
defaultLayout: 'layout',
helpers: {
//You can Name this anything
checkIfValuesOfJsonAreStrings: function(jsonList, context) {
for (var key in obj) {
if(typeOf obj[key] == "String"){
}else{
return context.inverse(this); // This False
}
}
return context.fn(this)
}
})
现在,在编写 .hbs 代码时,您可以包含
{{# checkIfValuesOfJsonAreStrings}}
<div>Yah all the values in JSON are string</div>
{{/checkIfValuesOfJsonAreStrings}}