Comparing version 0.1.5 to 0.1.6
{ | ||
"name": "nunjucks", | ||
"description": "A jinja inspired templating engine", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "James Long", |
@@ -23,4 +23,12 @@ | ||
The current version is v0.1.5 ([changelog](http://nunjucks.tumblr.com/post/33376448796/v0-1-5-macros-keyword-arguments-bugfixes)). | ||
## Documentation | ||
See http://nunjucks.jlongster.com/ for complete documentation. | ||
See http://nunjucks.jlongster.com/ for complete documentation. | ||
## Contributors | ||
* James Long (owner) | ||
* Brent Hagany | ||
* Thomas Hunkapiller |
@@ -135,3 +135,4 @@ | ||
if(typeof node.value == "string") { | ||
var val = node.value.replace(/"/g, '\\"'); | ||
var val = node.value.replace(/\\/g, '\\\\'); | ||
val = val.replace(/"/g, '\\"'); | ||
val = val.replace(/\n/g, "\\n"); | ||
@@ -155,5 +156,5 @@ val = val.replace(/\r/g, "\\r"); | ||
else { | ||
this.emit('context.lookup("' + name + '") || ' + | ||
'frame.lookup("' + name + '") || ' + | ||
'""'); | ||
this.emit('runtime.suppressValue(' + | ||
'runtime.contextOrFrameLookup(' + | ||
'context, frame, "' + name + '"))'); | ||
} | ||
@@ -241,8 +242,8 @@ }, | ||
compileLookupVal: function(node, frame) { | ||
this.emit('('); | ||
this.emit('runtime.suppressValue((('); | ||
this._compileExpression(node.target, frame); | ||
this.emit(')'); | ||
this.emit(')||{})'); | ||
this.emit('['); | ||
this._compileExpression(node.val, frame); | ||
this.emit(']'); | ||
this.emit('])'); | ||
}, | ||
@@ -281,3 +282,3 @@ | ||
this.emit('var ' + id + ' = '); | ||
this._compileExpression(node.value); | ||
this._compileExpression(node.value, frame); | ||
this.emitLine(';'); | ||
@@ -624,3 +625,3 @@ | ||
// var c = new Compiler(); | ||
// var src = '{% macro foo(x, y, z=3) %}h{% endmacro %}'; | ||
// var src = '{{ foo }}'; | ||
@@ -627,0 +628,0 @@ // var ns = parser.parse(src); |
@@ -38,2 +38,5 @@ var lib = require('./lib'); | ||
getFilter: function(name) { | ||
if(!this.filters[name]) { | ||
throw new Error('filter not found: ' + name); | ||
} | ||
return this.filters[name]; | ||
@@ -82,21 +85,54 @@ }, | ||
app.render = function(name, ctx, k) { | ||
var context = {}; | ||
if(app.render) { | ||
// Express >2.5.11 | ||
app.render = function(name, ctx, k) { | ||
var context = {}; | ||
if(lib.isFunction(ctx)) { | ||
k = ctx; | ||
ctx = {}; | ||
} | ||
if(lib.isFunction(ctx)) { | ||
k = ctx; | ||
ctx = {}; | ||
} | ||
context = lib.extend(context, app.locals); | ||
context = lib.extend(context, this.locals); | ||
if(ctx._locals) { | ||
context = lib.extend(context, ctx._locals); | ||
} | ||
if(ctx._locals) { | ||
context = lib.extend(context, ctx._locals); | ||
} | ||
context = lib.extend(context, ctx); | ||
context = lib.extend(context, ctx); | ||
var res = env.render(name, context); | ||
k(null, res); | ||
}; | ||
var res = env.render(name, context); | ||
k(null, res); | ||
}; | ||
} | ||
else { | ||
// Express <2.5.11 | ||
var http = require('http'); | ||
var res = http.ServerResponse.prototype; | ||
res._render = function(name, ctx, k) { | ||
var context = {}; | ||
if(this._locals) { | ||
context = lib.extend(context, this._locals); | ||
} | ||
if(ctx) { | ||
context = lib.extend(context, ctx); | ||
if(ctx.locals) { | ||
context = lib.extend(context, ctx.locals); | ||
} | ||
} | ||
var str = env.render(name, context); | ||
if(k) { | ||
k(null, str); | ||
} | ||
else { | ||
this.send(str); | ||
} | ||
}; | ||
} | ||
}, | ||
@@ -103,0 +139,0 @@ |
@@ -112,2 +112,13 @@ | ||
function suppressValue(val) { | ||
return (val !== undefined && val !== null) ? val : ""; | ||
} | ||
function contextOrFrameLookup(context, frame, name) { | ||
var val = context.lookup(name); | ||
return (val !== undefined && val !== null) ? | ||
val : | ||
frame.lookup(name); | ||
} | ||
module.exports = { | ||
@@ -117,3 +128,5 @@ Frame: Frame, | ||
makeKeywordArgs: makeKeywordArgs, | ||
numArgs: numArgs | ||
}; | ||
numArgs: numArgs, | ||
suppressValue: suppressValue, | ||
contextOrFrameLookup: contextOrFrameLookup | ||
}; |
@@ -19,2 +19,6 @@ var should = require('should'); | ||
it('should escape newlines', function() { | ||
render('foo\\nbar').should.equal('foo\\nbar'); | ||
}); | ||
it('should compile references', function() { | ||
@@ -30,2 +34,24 @@ var s = render('{{ foo.bar }}', | ||
it('should fail silently on undefined values', function() { | ||
var s = render('{{ foo }}'); | ||
s.should.equal(''); | ||
var s = render('{{ foo.bar }}'); | ||
s.should.equal(''); | ||
var s = render('{{ foo.bar.baz }}'); | ||
s.should.equal(''); | ||
var s = render('{{ foo.bar.baz["biz"].mumble }}'); | ||
s.should.equal(''); | ||
}); | ||
it('should not treat falsy values the same as undefined', function() { | ||
var s = render('{{ foo }}', {foo: 0}); | ||
s.should.equal('0'); | ||
var s = render('{{ foo }}', {foo: false}); | ||
s.should.equal('false'); | ||
}); | ||
it('should compile function calls', function() { | ||
@@ -62,2 +88,7 @@ var s = render('{{ foo("msg") }}', | ||
s.should.equal('good'); | ||
s = render('{% if food == "pizza" %}pizza{% endif %}' + | ||
'{% if food =="beer" %}beer{% endif %}', | ||
{ food: 'beer' }); | ||
s.should.equal('beer'); | ||
}); | ||
@@ -290,2 +321,8 @@ | ||
it('should compile set with frame references', function() { | ||
var s = render('{% set username = user.name %}{{ username }}', | ||
{ user: { name: 'james' } }); | ||
s.should.equal('james'); | ||
}); | ||
it('should throw errors', function() { | ||
@@ -292,0 +329,0 @@ (function() { |
@@ -6,3 +6,3 @@ | ||
var app = express(); | ||
var app = express.createServer(); | ||
@@ -9,0 +9,0 @@ var e = new env.Environment(new loaders.FileSystemLoader('views')); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
383420
10604
33
2