I'm using Node.js and trying to render an EJS template file. I figured out how to render strings:
var http = require('http'); var ejs = require('ejs'); var server = http.createServer(function(req, res){ res.end(ejs.render('Hello World')); }); server.listen(3000);
How can I render an EJS template file?
Use ejs. renderFile(filename, data) function with async-await. To render HTML files. To render EJS files.
EJS (Embedded JavaScript Templating) is one of the most popular template engines for JavaScript. As the name suggests, it lets us embed JavaScript code in a template language that is then used to generate HTML.
There is a function in EJS to render files, you can just do:
ejs.renderFile(__dirname + '/template.ejs', function(err, data) { console.log(err || data); });
Source: Official EJS documentation
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