-
Notifications
You must be signed in to change notification settings - Fork 13
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
Environment is not respected #2
Comments
I figured it out. I will post my solution here since it took my several hours to figure out. You need to pass the So, if you want to get nunjucks working both server- and client-side, you essentially need to specify the environment three times: once for the Node server, once for the precompiler, and once for the client-side. The easiest way to share your extensions both server and client-side is to put them into a separate JS file, like so: // nunjucks-ext.js
var _ = require('lodash');
module.exports = function(nj) {
return {
NgBind: function() {
this.tags = ['bind'];
this.parse = function(parser, nodes, lexer) {
var tag = parser.nextToken();
var args = new nodes.NodeList(tag.lineno, tag.colno);
var symbol = parser.nextToken();
args.addChild(new nodes.Literal(symbol.lineno, symbol.colno, symbol.value));
parser.advanceAfterBlockEnd(tag.value);
return new nodes.CallExtension(this, 'run', args);
};
this.run = function(context, symbol) {
return new nj.runtime.SafeString(_.template('<span ng-bind="<%- key %>"><%- val %></span>', {'key': symbol, 'val': context.ctx[symbol]}));
};
}
}
}; Note that I've defined the exports this way so that the nunjucks module has to be passed in. This way we can use the full library server-side and the slim version client-side, and still share the same copy of the extensions. The precompiler and Node can share the same environment. So put that in a separate file too: // nunjucks-env.js
var nj = require('nunjucks');
var _ = require('lodash');
var env = new nj.Environment(new nj.FileSystemLoader('app/views'), { autoescape: true });
_.forOwn(require('./app/scripts/nunjucks-ext')(nj), function (ext, name) {
env.addExtension(name, new ext);
});
module.exports = env; Then in your Node application, you can simply require your environment and use it immediately: // app.js
var njEnv = require('./nunjucks-env');
njEnv.render('hello.njs', { name: 'Mark' }) Lastly, you can configure the environment for client like so: // nunjucks-client.js
var _ = require('lodash');
require('./nunjucks-templates');
require('../../node_modules/nunjucks/browser/nunjucks-slim');
var njEnv = new nunjucks.Environment(null, { autoescape: true });
_.forOwn(require('./nunjucks-ext')(nunjucks), function (ext, name) {
njEnv.addExtension(name, new ext);
});
console.log(njEnv.render('app/views/hello.njs', {name: 'Ralph'})); (Note that we need to specify the full template path until Issue #1 is resolved) This file can be browserified to pull in the needed requirements. I've used lodash here, but you can easily rewrite it without. |
Thanks a lot for this (unfortunately missed some of the issues on this repo until now). Precompilation is hard for these reasons, and I'm sorry it took you a while to figure it out. I can make the docs clearer about that. The problem is that You only really feel this pain if you are using extensions, because otherwise don't need to pass an Thanks for the detailed write-up! |
Perhaps I'm doing something wrong, but I can't figure it out.
The documentation says we need to use the
env
option when using extensions.So, I've configured my Gruntfile like this,
Where
njEnv
is defined like this:I then ran the Grunt task, which spits out a file like this:
(I added in the
console.log
)And ran it in my browser, via the JavaScript console:
But it just gives me the following error:
Further, from the log, I can see that
autoesc
is false, which means it's not even respecting the options I passed in.Why not? Aren't my settings supposed to be copied into the precompilation?
The text was updated successfully, but these errors were encountered: