BadIdeaException
BadIdeaException

Reputation: 2124

Assign Pug mixin to variable

Is there any way assigning a Pug mixin to a variable? (N.b. not the results of the mixin.)

I.e. something like this:

mixin a
    ...

mixin b
    ...

mixin c
    ...

-
    let myMixin;
    switch (someCondition) {
        case 1: myMixin = b; break;
        case 2: myMixin = c; break;
        default: myMixin = a;
    }

...And then use it like this further down in the template:
   +myMixin
    
    

Upvotes: 2

Views: 678

Answers (1)

biodiscus
biodiscus

Reputation: 880

You can use dynamic mixins in Pug:

+#{mixinName}()

Create a mixin which generate a dynamic mixin, e.g.:

mixin dynMixin(name)
  +#{name}()

Then use dynamic mixin easy:

mixin a
  p AAA

mixin b
  p BBB

mixin c
  p CCC

-
  let myMixin;
  let someCondition = 2;
  switch (someCondition) {
    case 1: myMixin = 'b'; break;
    case 2: myMixin = 'c'; break;
    default: myMixin = 'a';
  }

+dynMixin(myMixin)
//- or write directly: +#{myMixin}()

Note: The name of dynamic mixin must be a string.

Upvotes: 2

Related Questions