Search code examples
javascriptpugstring-interpolation

How to use reverse interpolation pug.js


I have a pug.js that has a good amount of code included in it. In order to write the block of code I have to write

pre #[code.language-python print("Hello!")]

I know that I can write a variable and use it like this:

- var codeMsg = 'Hello!'

h1 #{codeMsg}

but is there a way to use it so that I write a variable that contains the long tag declaration, and then put in my code? Like this:

myNewTag print("Hello!")

Thanks for any help!


Solution

  • You can combine a mixin with tag interpolation like this:

    // mixin declaration
    mixin python
      pre
        code.language-python
          block
    
    // usage (don't forget the trailing dot)
    +python.
      print("Hello, World!")
      print("Goodbye, World!")