问题描述
我有一个混合类型属性的对象——一些字符串、一些字符串数组、一些包含字符串数组的对象——它们可能会深入很多层次.
我想遍历所有属性,以便一个对象创建一个 div,一个数组创建一个 div,一个字符串属性创建一个包含文本的范围.
{ "string" : "some text", "object" : { "array" : [ "text" ] } }
上述对象将呈现为:
一些文字<div><div><span>文本</span>
我有一个混合类型属性的对象——一些字符串、一些字符串数组、一些包含字符串数组的对象——它们可能会深入很多层次.
我想遍历所有属性,以便一个对象创建一个 div,一个数组创建一个 div,一个字符串属性创建一个包含文本的范围.
{ "string" : "some text", "object" : { "array" : [ "text" ] } }
上述对象将呈现为:
一些文字<div><div><span>文本</span>
但通常结构要复杂得多.我该怎么去完成这个是玉?
你问这个问题已经有一段时间了,但我认为 mixin
是你的朋友.我还没有尝试过,但如果 mixins 支持递归,这应该可行:
mixin parseObject(obj)div- 每个 val, obj 中的键- if (typeof val === 'string')跨度#{val}- else if (typeof val === 'object')混合 parseObject(val)
然后在 .jade 文件的正文中,调用 mixin parseObject(rootObject)
.
I have an object of mixed type properties - some strings, some arrays of strings, some objects containing arrays of strings - that can potentially go many levels deep.
I would like to iterate over all properties so that an object creates a div, an array creates a div, and a string property creates a span to contain the text.
{ "string" : "some text", "object" : { "array" : [ "text" ] } }
The above object would render as:
<span>some text</span>
<div>
<div>
<span>text</span>
</div>
</div>
But usually much more complex structures. How should I go about accomplishing this is Jade?
It's been a while since you asked, but mixin
is your friend, I think. I haven't tried it out, but if mixins support recursion, this should work:
mixin parseObject(obj)
div
- each val, key in obj
- if (typeof val === 'string')
span #{val}
- else if (typeof val === 'object')
mixin parseObject(val)
Then in the body of your .jade file, call mixin parseObject(rootObject)
.
这篇关于对 Jade 模板中的对象进行递归迭代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!