【发布时间】:2018-01-13 22:06:28
【问题描述】:
我正在使用 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 模板引擎来实现这一点,因为我想在我的项目中维护一个模板引擎。
谢谢
【问题讨论】:
标签: javascript node.js templates pug mustache