2

Pug docs say that it can transform

doctype html
html(lang="en")
  head
    title= pageTitle
    script(type='text/javascript').
      if (foo) bar(1 + 5)
  body
    h1 Pug - node template engine
    #container.col
      if youAreUsingPug
        p You are amazing
      else
        p Get on it!
      p.
        Pug is a terse and simple templating language with a
        strong focus on performance and powerful features.

to

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Pug</title>
    <script type="text/javascript">
      if (foo) bar(1 + 5)
    </script>
  </head>
  <body>
    <h1>Pug - node template engine</h1>
    <div id="container" class="col">
      <p>You are amazing</p>
      <p>Pug is a terse and simple templating language with a strong focus on performance and powerful features.</p>
    </div>
  </body>
</html>

But notice in the resulting HTML there is a script file inside it. That is not what I want. Can't I generate HTML result such that the script is also evaluated? For example if there is DOM manipulation logic inside, the result HTML should reflect that instead of showing script tag.

  • 1
    Pugjs has the ability to conditionally render the output HTML https://pugjs.org/language/conditionals.html – MarkoCen Jul 15 '21 at 15:15
  • Does this answer your question? [How can I render inline JavaScript with Jade / Pug?](https://stackoverflow.com/questions/5858218/how-can-i-render-inline-javascript-with-jade-pug) – urosc Jul 15 '21 at 16:29
  • 2
    @ross-u nope as OP says he wants to evaluate JS inside HTML and get final HTML without script tag inside it, your link still has script tag inside HTML – Giorgi Moniava Jul 15 '21 at 19:28

1 Answers1

2

Pug is rendered server-side; there is no DOM while Pug is being compiled. If you want to do DOM manipulation after Pug compiles but before the compiled HTML is sent from the server to the client, you'll need to pipe it through a server-side DOM emulator like JSDom. Pug won't do that on its own—that's not what it's meant for.

Sean
  • 6,873
  • 4
  • 21
  • 46