D
D
drtvader2016-06-27 16:56:26
Pug
drtvader, 2016-06-27 16:56:26

How to write a mixin for Jade/Pug?

Hey! How to write a mixin correctly, having the condition that in any of the li there may be another ul list.
Now I wrote this mixin:

mixin benefits-product(title, ...items)
    +b.each-product
        +e('ul').list #{title}:
            each item in items
                +e('li').item--benefits #{item}
            +e('li').item--benefits #{item}
                +e('ul').list--benefits
                    each item in items
                        +e('li').item--benefits #{item}

We1bi.png
But he is not faithful.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Velichko, 2016-07-12
@drtvader

Recursion
codepen.io/Zoxon/pen/zBPBbW?editors=1000#0

{
  "title": "Title text",
  "items": [
    {
      "title": "Item 1"
    },
    {
      "title": "Item 2",
      "sublevel": true,
      "items": [
        {
          "title": "Item 2.1"
        },
        {
          "title": "Item 2.2"
        },
        {
          "title": "Item 2.2",
          "sublevel": true,
          "items": [
            {
              "title": "Item 2.2.1",
              "sublevel": true,
              "items": [
                {
                  "title": "Item 2.2.2.1"
                },
                {
                  "title": "Item 2.2.2.2"
                },
                {
                  "title": "Item 2.2.2.3"
                }
              ]
            },
            {
              "title": "Item 2.2.2"
            },
            {
              "title": "Item 2.2.3"
            }
          ]
        }
      ]
    },
    {
      "title": "Item 3"
    }
  ]
}

mixin list(data)
  ul
    each item in data.items
      li= item.title
        if item.sublevel
          +list(item)

You can generally remove sublevel and check for nested items

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question