daveBeginner
daveBeginner

Reputation: 21

generate HTML string from pug which contains also scripts

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.

Upvotes: 2

Views: 786

Answers (1)

Sean
Sean

Reputation: 8031

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.

Upvotes: 2

Related Questions