Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document ejs.renderFile() method #52

Open
recidive opened this issue Jun 5, 2012 · 8 comments
Open

Document ejs.renderFile() method #52

recidive opened this issue Jun 5, 2012 · 8 comments

Comments

@recidive
Copy link

recidive commented Jun 5, 2012

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.

@apptous-seb
Copy link

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);
          }
      });
    }

@tj
Copy link
Owner

tj commented Aug 22, 2012

@apptous-seb it's for node lol not the client :p

@apptous-seb
Copy link

@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

@tj
Copy link
Owner

tj commented Aug 22, 2012

no, fs.readFileSync is a node method

@apptous-seb
Copy link

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?

@tj
Copy link
Owner

tj commented Aug 22, 2012

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

@apptous-seb
Copy link

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?

@tj
Copy link
Owner

tj commented Aug 23, 2012

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..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants