【问题标题】:generate HTML string from pug which contains also scripts从 pug 生成 HTML 字符串,其中还包含脚本
【发布时间】:2021-09-24 11:13:54
【问题描述】:

Pug 文档说它可以变形

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.

<!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>

但请注意,在生成的 HTML 中,其中有一个脚本文件。那不是我想要的。我不能生成 HTML 结果以便脚本也被评估吗? 例如,如果里面有 DOM 操作逻辑,结果 HTML 应该反映它而不是显示脚本标签。

【问题讨论】:

标签: javascript node.js pug


【解决方案1】:

Pug 在服务器端呈现;编译 Pug 时没有 DOM。如果你想在 Pug 编译之后但在编译后的 HTML 从服务器发送到客户端之前进行 DOM 操作,你需要通过像 JSDom 这样的服务器端 DOM 模拟器来处理它。 Pug 不会自己这样做——这不是它的本意。

【讨论】:

    猜你喜欢
    • 2021-10-22
    • 1970-01-01
    • 2015-01-26
    • 2019-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多