如何在 lodash 模板中传递对象键(带有特殊字符或空格)?
how to pass object key(with special character or spaces) in lodash template?
我们可以像模板中的键一样传递数组
var compiled = _.template('<%= [hello] %>')({ 'hello': 'how are you'});
console.log(compiled);// how are you
或喜欢对象键
var compiled = _.template('<%= hello %>')({ 'hello': 'how are you' });
console.log(compiled);//how are you
如何传递一些特殊字符的键名?
var compiled = _.template("<%= ['hell:-o'] %>")({ 'hell:-o': 'how are you' });
和多维数组一样?
var compiled = _.template("<%= [hello][hello] %>")({ 'hello': {'hello': 'how are you'} });
默认情况下,整个对象在模板中作为 obj
可用,因此您可以像这样执行 #3:
> _.template("<%= obj['hell:-o'] %>")({ 'hell:-o': 'how are you' });
"how are you"
您甚至可以更改 docs 中提到的 this 变量的名称:
_.template("<%= data['hell:-o'] %>", {variable: "data"})({ 'hell:-o': 'how are you' });
"how are you"
对于 #4,您可以像在 JS 中一样访问它:
> _.template("<%= hello.hello %>")({ 'hello': {'hello': 'how are you'} });
"how are you"
我们可以像模板中的键一样传递数组
var compiled = _.template('<%= [hello] %>')({ 'hello': 'how are you'});
console.log(compiled);// how are you
或喜欢对象键
var compiled = _.template('<%= hello %>')({ 'hello': 'how are you' });
console.log(compiled);//how are you
如何传递一些特殊字符的键名?
var compiled = _.template("<%= ['hell:-o'] %>")({ 'hell:-o': 'how are you' });
和多维数组一样?
var compiled = _.template("<%= [hello][hello] %>")({ 'hello': {'hello': 'how are you'} });
默认情况下,整个对象在模板中作为 obj
可用,因此您可以像这样执行 #3:
> _.template("<%= obj['hell:-o'] %>")({ 'hell:-o': 'how are you' });
"how are you"
您甚至可以更改 docs 中提到的 this 变量的名称:
_.template("<%= data['hell:-o'] %>", {variable: "data"})({ 'hell:-o': 'how are you' });
"how are you"
对于 #4,您可以像在 JS 中一样访问它:
> _.template("<%= hello.hello %>")({ 'hello': {'hello': 'how are you'} });
"how are you"