我正在使用Node.js开发聊天应用程序,使用Pug模板引擎。当我尝试渲染可重用的模板时,遇到了困难,而这是我在使用Mustache模板引擎时实现的内容。
下面是一个使用Mustache的示例,展示我希望在Pug中实现的功能:
无论用户在表单中输入了什么,都会动态显示出来。我的问题是,我如何使用Pug模板引擎实现这一点,因为我希望在整个项目中保持一致的模板引擎。
谢谢。
下面是一个使用Mustache的示例,展示我希望在Pug中实现的功能:
//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"></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模板引擎实现这一点,因为我希望在整个项目中保持一致的模板引擎。
谢谢。