pug

Is this a pug template bug or my misunderstanding of the grammar?


I have the following code:

- var a = 5;
- var b = 3;
script
  if (a === 5)
    if (b === 2)
    .
      var x = true;
  include script.js

I want to include the script.js file when a == 5 and if b === 2, I want to insert a new line var x = true above, inside the tag.

However, when I use the code, the x = true is always inserted.

enter image description here

You can verify it here:

https://pugjs.org/language/includes.html


Solution

  • You just need to indent the . and var x lines one more level so that they're within the second conditional:

    - var a = 5;
    - var b = 3;
    script
      if (a === 5)
        if (b === 2)
          .
            var x = true;
      include script.js
    

    If you want x to always be declared and be either true or false based on your conditions, another approach would be something like this:

    - var a = 5
    - var b = 3
    script
      | var x = #{a === 5 && b === 2}
      include script.js
    

    This will make x true if the Pug variables a and b are 5 and 2 respectively, and false if not.