javascript - 在 Node/Express 中使用 Pug 模板引擎进行动态模板渲染

标签 javascript node.js templates pug mustache

我正在使用 Node.js 开发一个聊天应用程序,我正在使用 Pug 模板引擎,当我尝试渲染一个可重用的模板时卡住了,这是我使用 Mustache 模板引擎实现的。

以下是我想用 Pug 实现的示例,此示例中使用了 Mustache

//index.js
  socket.on('newMessage', message => {
  let template = jQuery('#message-template').html();
  let html = Mustache.render(template, {
    text: message.text,
    from: message.from
  });

  jQuery('#messages').append(html)
});

输出结果的 index.html 文件片段

<div class="chat__main">
    <ol id="messages" class="chat__messages"></ol>

    <div class="chat__footer">
        <form action="" id="message-form">
            <input type="text" name="message" placeholder="Message" autofocus autocomplete="off">
            <button>Send</button>
        </form>
        <button id="send-location">Send location</button>
    </div>

    <script id="message-template" type="text/template">
        <p>{{text}}</p>
    </script>


</div>

<script src="/socket.io/socket.io.js"></script>
<script src="javascripts/libs/jquery-3.2.1.min.js"></script>
<script src="javascripts/libs/moment.js"></script>
<script src="javascripts/libs/mustache.js"></script>
<script src="javascripts/index.js"></script>
</body>
</html>

无论表单中的用户输入是动态显示的,我的问题是,我如何使用 Pug 模板引擎实现这一点,因为我想在我的项目中维护一个模板引擎。

谢谢

最佳答案

您可以使用 pug.compileFileClient ,您可能希望以自动化方式执行 compile 步骤(gulpgrunt、...)

Compile a Pug template file to a string of JavaScript that can be used client side along with the Pug runtime.

First, our template file.

h1 This is a Pug template
h2 By #{author}

Then, we compile the Pug file into a function string.

var fs = require('fs');
var pug = require('pug');

// Compile the template to a function string
var jsFunctionString = pug.compileFileClient('/path/to/pugFile.pug', {name: "fancyTemplateFun"});

// Maybe you want to compile all of your templates to a templates.js file and serve it to the client
fs.writeFileSync("templates.js", jsFunctionString);

Here’s what the output function string looks like (written to templates.js).

function fancyTemplateFun(locals) {
  var buf = [];
  var pug_mixins = {};
  var pug_interp;

  var locals_for_with = (locals || {});

  (function (author) {
    buf.push("<h1>This is a Pug template</h1><h2>By "
      + (pug.escape((pug_interp = author) == null ? '' : pug_interp))
      + "</h2>");
  }.call(this, "author" in locals_for_with ?
    locals_for_with.author : typeof author !== "undefined" ?
      author : undefined)
  );

  return buf.join("");
}

Be sure to send the Pug runtime (node_modules/pug/runtime.js) to the client in addition to the template that you just compiled.

<!DOCTYPE html>
<html>
  <head>
    <script src="/runtime.js"></script>
    <script src="/templates.js"></script>
  </head>

  <body>
    <h1>This is one fancy template.</h1>

    <script type="text/javascript">
      var html = window.fancyTemplateFun({author: "enlore"});
      var div = document.createElement("div");
      div.innerHTML = html;
      document.body.appendChild(div);
    </script>
  </body>
</html>

关于javascript - 在 Node/Express 中使用 Pug 模板引擎进行动态模板渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48244718/

相关文章:

javascript - 无法使用 Node 运行 babel 转译文件

node.js - 手写笔在 Node.js 下突然无法工作

c++ - CMake 生成的 DLL 和 Curiously Recurring 模板 (C++) 的不正确行为

javascript - Firefox 在哪里存储 javascript/HTML localStorage?

javascript - 需要帮助根据元素的宽度定位元素 (Javascript)

javascript - 单击时的 CSS3 关键帧动画(使用 addClass)。如何通过添加 css 类重新启动 CSS3 动画?

javascript - 如何从 JavaScript 中的转发器获取值?

javascript - JSON (node/express) 和 JSON 数组中的自定义 "types"

c++ - 专用 vector 模板的大括号初始化

c++ - 推导重载函数的类型 - currying