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

Possible to access Node.js functionality inside a template? #37

Closed
UweKeim opened this issue Apr 27, 2014 · 3 comments
Closed

Possible to access Node.js functionality inside a template? #37

UweKeim opened this issue Apr 27, 2014 · 3 comments

Comments

@UweKeim
Copy link

UweKeim commented Apr 27, 2014

While currently evaluating your (great!) engine inside a Node.js test project, I tried the following (although not sure whether I really need it) inside a .vash template file:

@{
    var fs = require('fs');

    // ...more code...
}

This results in an error message

require is not defined.

My question: Is this something that is simply not possible and/or am I totally misusing the idea of a template engine?

@kirbysayshi
Copy link
Owner

So, you really shouldn't need this within a template, although it can be possible. The reason why it doesn't work is because require is provided for each module in nodejs, and is not actually a global. To make this work, you could so something like:

@{ var fs = model.require('fs'); }
tpl({ require: require })

Or if you set useWith: true, you could just reference require like before.

But even though this is possible, it's probably not something you should do. Usually it's better to pass everything into the template. But maybe you're doing something super cool! In that case carry on!

@UweKeim
Copy link
Author

UweKeim commented Apr 27, 2014

Thanks a lot for helping me on this. I strongly doubt that what I'm doing is actually that cool that it needs something so unusual 😉.

@UweKeim
Copy link
Author

UweKeim commented Apr 29, 2014

Just adding a clarification for myself about what the useWith does:

This plugin configures Vash to use its useWith mode, meaning that the members of the template data are available to the razor view as the default object.

(Quote from here)

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

2 participants