Recursive iteration over an object in Jade template?

前端 未结 3 1570
一个人的身影
一个人的身影 2021-02-05 14:32

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

相关标签:
3条回答
  • 2021-02-05 14:50

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

    0 讨论(0)
  • 2021-02-05 15:02

    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 )

    0 讨论(0)
  • 2021-02-05 15:04

    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)
    
    0 讨论(0)
提交回复
热议问题