NodeJS- EJS
EJS template
const template = "hello <% echo('world'); %>";
Within EJS template, <% %>
is a scriptlet tag, which executes JS code.
ejs.render ?
const ejs = require("ejs");
// Data object (empty in this case)
const data = {};
// Options object specifying the custom output function name
const options = { outputFunctionName: 'echo' };
// Rendering the template using EJS
const rendered = ejs.render(template, data, options);
When ejs.render
exectues, two things happen.
- It evaluates the template.
- The scriptlet
<% echo ('world') %>
executes the function namedecho
Template Output: The visible output "hello world" indeed comes from the template. The static part "hello " is directly outputted, while the dynamic part "world" is generated by the echo function defined in the template.
Purpose of options: The options object ({ outputFunctionName: 'echo' }) allows you to customize how EJS interprets and executes the template. By specifying outputFunctionName, you control which function within the template handles dynamic content generation.
So in my dummy words:
The options object tells the render function which function name should be recognized and executed within the template.