Search code examples
pug

How to put the same logic of different pug files into one place?


I have three pug files: a.pug, b.pug and c.pug:

a.pug:

block header
  style
    .
      body {...}
    if (option.a == 'yes')
      include a.css
    if (option.b == 'yes')
      include b.css
  #main-a
    p This is page A ....

b.pug:

block header
  style
    .
      body {...}
    if (option.a == 'yes')
      include a.css
    if (option.b == 'yes')
      include b.css
  #main-b
    p This is page B ....

c.pug:

block header
  style
    .
      body {...}
    if (option.a == 'yes')
      include a.css
    if (option.b == 'yes')
      include b.css
  #main-c
    p This is page C ....

They share the same logic under the style block:

    if (option.a == 'yes')
      include a.css
    if (option.b == 'yes')
      include b.css

Is there any way to make the logic in a specific file so I can include it from my three pug files later? Otherwise, if I need to modify the logic, like adding a new line like:

    if (option.c == 'yes')
      include c.css

I have to make changes to all three pug files.

Thanks for any tips.


Solution

  • You can put the logic in an external file. The variables can be processed from there as well.

    a.pug, b.pug, c.pug, ...

    block header
      include styles.pug
      #main-a
        p This is page ....
    

    styles.pug

    style.
      body {...}
      if (option.a == 'yes')
        include a.css
      if (option.b == 'yes')
        include b.css