node.js - How can I pass variable to ejs.compile -
my bottom_index.ejs looks that:
<div>the bottom section</div>
in code declare ejs:
ejs = require('ejs');
then compile function:
var botom_index_ejs = ejs.compile(fs.readfilesync(__dirname + "/../views/bottom_index.ejs", 'utf8'));
and call rendered html:
botom_index_ejs()
it works fine!
now change template to:
<div><%= bottom_text %></div>
and able pass parameter (bottom_text) bottom_index.ejs
how should pass parameters?
thanks!
parameters passed ejs template js plain object. example sholud be:
botom_index_ejs({ bottom_text : 'the bottom section' });
update:
test.js
var fs = require('fs'); var ejs = require('ejs'); var compiled = ejs.compile(fs.readfilesync(__dirname + '/test.ejs', 'utf8')); var html = compiled({ title : 'ejs', text : 'hello, world!' }); console.log(html);
test.ejs
<html> <head> <title><%= title %></title> </head> <body> <p><%= text %></p> </body> </html>
Comments
Post a Comment