I'm working on a chat app in Node.js, I'm using Pug Template Engine, I got stuck when I tried to render a reusable template, something I achieved with Mustache Template Engine.
Below is the example of what I'd like to achieve with Pug, Mustache is used in this example
//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)
});
A fragment of my index.html file outputting the result
<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>
Whatever the user input in the form is dynamically displayed, my question is, how can I achieve this using Pug Template Engine, because I'd like to maintain a Template Engine across my project.
Thanks
You can use pug.compileFileClient, you might want to do the compile step in an automated way (gulp, grunt, ...)
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With