Open recidive opened 12 years ago
Does renderFile() method work at all? It seems that it uses 'fs' object which is defined as {} (empty object). For now I used following method to render a template from file:
function insertTemplate(data) {
getTemplate('/tpl/template.ejs', function addTemplate(err, tpl) {
if (err) {
throw err;
}
var tpl = require('ejs').render(tpl, {data: data});
$(tpl).prependTo('div.dataItems');
});
}
function getTemplate(file, callback) {
$.ajax(file, {
type: 'GET',
success: function(data, textStatus, xhr) {
return callback(null, data);
},
error: function(xhr, textStatus, error) {
return callback(error);
}
});
}
@apptous-seb it's for node lol not the client :p
@visionmedia I am about the ejs.js in the root folder. Isn't it for the client side? See https://github.com/visionmedia/ejs/blob/master/ejs.js#L312
no, fs.readFileSync
is a node method
Yes, I understand that. And understand fs.readFileSync doesn't work on client. And "fs" object itself is present in the client script, but it is a mock empty object:
function require(p){
if ('fs' == p) return {};
...
And now the renderFile() method can not work in client side version of ejs.js. So the question is why renderFile() and mock "fs" object are present in the client-side script? Maybe it should not be there at all? Or this is for future enhancement? As I see "fs" is only used in renderFile(), so it is possible to implement fs.readFileSync for client side and read template from the server. And maybe it is better implement fs.readFile with callback (and renderFile will also have callback parameter) - something like example I shown above.
P.S. Sorry for confusion, I worked on client side today and did not have any doubts that this issue is about the client side ejs ))) If there is a sense to continue discussion then maybe I should open another ticket?
so it doesn't error on fs.something
, that's the only reason it stubs that out. It just complicates the client-side build if you start adding conditional bodies of code etc
OK, I see now. So what do you think about the changing fs.readFileSync to fs.readFile in the renderFile() method and providing the client-side version of fs.readFile()? I could implement this and do a pull request. Or maybe I just miss something and there is an easy way to use the same template on the server and on the client?
I don't see a point, it's (usually) a poor choice to do additional requests just for templates, it does depend though, but I think that's not a concern for EJS, just do XHR if you need to etc..
The method renderFile() method is not documented in Readme.md file.
Please advise if this is not the best place to report this kind of issues.
Thanks.