Recursive iteration over an object in Jade template?

放肆的年华 提交于 2019-12-03 03:23:08
mna

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).

Sunrise

In the modern version of Jade it's look like

mixin parseObject( obj )
  div
    each val in obj
      if typeof val === 'string'
        span= val
      else if typeof val === 'object'
        +parseObject( val )

Then in the body of your .jade file, call

+parseObject( rootObject )

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.

mixin parseObject(obj)
  div
    each val, key in obj
      if typeof val === 'string'
        span #{val}
      else if typeof val === 'object'
        mixin parseObject(val)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!