renderFile to accept a file as input
dbauszus-glx opened this issue · comments
I am having a problem with relative paths not being supported by my cloud provider. I can get the file alright with fs.readFileSync but I cannot use the './' path syntax. While probably not possible in the short run is there any way to provide a file for the renderFile method?
In fact renderFile()
does accept absolute paths as well as relative ones. For example if you take a look at this line of the jsrender-node-starter sample app.
You can replace
var html = jsrender.renderFile('./templates/layout-hello.html', { hello: "world" });
by
var html = jsrender.renderFile(process.cwd() + '/templates/layout-hello.html', { hello: "world" });
or, you can do:
var dn = __dirname;
app.get('/hello/world', function(req, res) {
var html = jsrender.renderFile(dn + '/templates/layout-hello.html', { hello: "world" });
...
};
All three will work in the same way - and the second and third are using absolute paths.
If you really need to get the file first, from readFileSync(filePath, encoding)
then you can take the returned string, (which is the markup for your template), and compile it as a template and render it against data as follows:
var markup = Fs.readFileSync(filePath, "utf8");
var tmpl = jsrender.templates(markup);
var html = tmpl.render({ hello: "world" });
Note though that the jsrender.templates()
accepts file paths, as well as markup strings, but in this case (unlike renderFile()
) the file path does indeed have to be relative, starting with ./
I didn't hear back on this. Closing for now.