javascript - Jade(Pug) Html 渲染输出

标签 javascript html node.js pug

我最近在 Mac OS X Yosemite 上安装了 Jade(Pug)。

我安装了最新版本的 node.js,然后使用终端命令:$ sudo npm install pug-cli -g

一切都很好,直到我不得不渲染文件。我用默认的 pug 代码创建了一个 test.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.

然后使用终端渲染它来测试它。我使用了:$ pug -P test.pug 并将其呈现给 test.html,输出如下所示:

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

好吧,现在当我想自动渲染它并使用 -watch 功能时: $ pug -w test.pug 它输出如下:

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

我找不到解决这个问题的方法。对于我在 youtube 或其他教程上观看的其他人来说,输出看起来具有正确的 HTML 结构,但我的呈现为缩小版本。

我该怎么做才能解决这个问题并让它自动呈现为 HTML 格式的正确输出?

最佳答案

您在第一个变体 (-P) 中设置的选项启用输出美化。如果你想在第二个变体上使用它,只需添加标志:pug -P -w test.pug

来自文档:

-h, --help             output usage information
-V, --version          output the version number
-O, --obj <path|str>   JavaScript options object or JSON file containing it
-o, --out <dir>        output the compiled html to <dir>
-p, --path <path>      filename used to resolve includes
-P, --pretty           compile pretty html output
-c, --client           compile function for client-side runtime.js
-n, --name <str>       the name of the compiled template (requires --client)
-D, --no-debug         compile without debugging (smaller functions)
-w, --watch            watch files for changes and automatically re-render
-E, --extension <ext>  specify the output file extension
--name-after-file      name the template after the last section of the file path
                       (requires --client and overriden by --name)
--doctype <str>        specify the doctype on the command line (useful if it
                       is not specified by the template)

关于javascript - Jade(Pug) Html 渲染输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38303672/

相关文章:

php - 使用 laravel 在导航栏上时注销链接不起作用

Node.js 2 要求()

javascript - 属性 <Object> 包含无效的嵌套实体。/Firebase

javascript - 被我收到的 d3 错误难住了 - 错误 : <path> attribute d: Expected number, "M0,NaNL21.923076923…"

javascript - 如何查找导致pm2重启的错误

javascript - 确定哪些 JavaScript 函数未被调用

html - 导航栏中的中心表单

html - 如何使用多色主题设置做 HTML 模板?

node.js - 如何使用 nodeJS 和 protobufjs 在 MQTT 中发送缓冲区或字节数组

javascript - 如何设置流nodejs读取的行数限制