I
I
inkShio2019-02-03 01:13:52
Pug
inkShio, 2019-02-03 01:13:52

How to implement multiple blocks in mixin - pug?

There is such a mixin for a button

- var blocks={};

mixin set(key)
  - blocks[key] = this.block

mixin A(buttonClass, buttonText, disabled)
  button.button(class= buttonClass, disabled= disabled)
    span
      if block
        block
        - blocks.left()
        | #{buttonText}
        - blocks.right()
      else
        | #{buttonText}

+A("button_small button_filled", "Кнопка", false)
  +set('left')
    p l
  +set('right')
    p r

This mixin works, but it doesn't work as well as I would like. I can't figure out how to fix it.
The idea is that set('left') is for inserting an icon on the left into the button, +set('right') is an icon on the right.
If I write +set('left') and +set('right') as an example, then everything is ok. But if I write one thing, then it's a mistake.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2019-02-03
@inkShio

if I write one thing, then an error

So you check before calling the function that it exists. Instead
will
-
  if (blocks.left) {
    blocks.left()
  }

Well, or shorter, you can - blocks.left && blocks.left():.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question