Jade 模板中的对象的递归迭代?
我有一个混合类型属性的对象 - 一些字符串,一些字符串数组,一些包含字符串数组的对象 - 可能会深入很多级别。
我想迭代所有属性,以便对象创建 div,数组创建 div,字符串属性创建包含文本的 span。
{ "string" : "some text", "object" : { "array" : [ "text" ] } }
上面的对象将呈现为:
<span>some text</span>
<div>
<div>
<span>text</span>
</div>
</div>
但通常结构要复杂得多。我应该如何去完成这件事是玉?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你问这个问题已经有一段时间了,但我想
mixin
是你的朋友。我还没有尝试过,但如果 mixins 支持递归,这应该可以工作:然后在 .jade 文件的正文中,调用
mixin parseObject(rootObject)
。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:Then in the body of your .jade file, call
mixin parseObject(rootObject)
.现在好像支持递归了。我已经成功地使用了该功能,并进行了一些小的调整;调用该函数时需要使用 mixin 关键字。
Recursion seems to be suppported now. I have successfully used the function with a minor tweak; you need to use the mixin keyword when calling the function.
在现代版本的 Jade 中,它看起来像
然后在 .jade 文件的正文中,调用
+parseObject( rootObject )
In the modern version of Jade it's look like
Then in the body of your .jade file, call
+parseObject( rootObject )