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.

3
  • 1
    Pugjs has the ability to conditionally render the output HTML pugjs.org/language/conditionals.html Commented Jul 15, 2021 at 15:15
  • Does this answer your question? How can I render inline JavaScript with Jade / Pug? Commented Jul 15, 2021 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 Commented Jul 15, 2021 at 19:28

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.