F
F
feelsgood2017-03-22 23:59:38
Pug
feelsgood, 2017-03-22 23:59:38

Mixin vs. Include in Pug?

What is the best approach for reusing different markup structures in Pug.js, for example when using a component approach in layout? Let's say we have a button that could be:

button.btn.btn--default Submit
button.btn.btn--disable Send
...

It's better to use a mixin, something like:
mixin btn(type)
  button.btn(class=`btn--${type}`) Text
and stick it in the right places:
+btn('default')
+btn('disable')

Or is it better to create files with the desired markup and paste with include?
btn-default.pug
btn-disable.pug

It is clear that the matter is not limited to buttons alone and it is supposed to reuse more complex markup

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Kitmanov, 2017-03-23
@feelsgood

Parameters are explicitly passed to the mixin, and this is a great happiness. If you try to emulate mixins with includes, you will quickly run into code like this:

- var buttonTitle = "кнопка";
- var buttonSmall = "small";
- var buttonType = "danger";
- var icon = "trash";
include 'includes/button'

Imagine two or three of these buttons side by side. Do you still want to do it with includes? And if you need to transfer custom layout to the button? With inclusions, this is not possible at all.
Inclusions can only connect these same mixins, IMHO.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question