First, let's create a template to be rendered!
p Hello World, #{name}!
Save this in a file ending with the .pug
extension (you can call it anything you like, but we will use view.pug
in the following code to compile it).
All that's left to do, now, is compile that template! Create a JS script file (we'll call ours main.js
), and add the following content:
// Import the pug module
const pug = require('pug');
// Compile the template (with the data not yet inserted)
const templateCompiler = pug.compileFile('view.pug');
// Insert your data into the template file
console.log(templateCompiler({ name: 'John' });
When you run this file with npm main.js
, you should get the following HTML code output in your console:
<p>Hello World, John!</p>
Congratulations, you just created and compiled your first template! On to more advanced stuff, such as Conditionals, Iteration, and much more!