Last active
August 29, 2015 14:16
-
-
Save judas-christ/f7271bda31250d8c877f to your computer and use it in GitHub Desktop.
Swig template bug with chained instance functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var swig = require('swig'); | |
var assert = require('assert'); | |
swig.invalidateCache(); | |
var failingTemplate = '{% for child in model.getRoot().getChildren() %}{{ child.getName() }}{% endfor %}'; | |
var workingTemplate = '{% for child in model.getChildren() %}{{ child.getName() }}{% endfor %}'; | |
var expected = 'child'; | |
function Model(name, children) { | |
assert(this instanceof Model, 'this is not an instance of Model') | |
this.name = name; | |
this.children = children || []; | |
}; | |
Model.prototype = { | |
getRoot: function() { | |
assert(this instanceof Model, 'this is not an instance of Model') | |
return model; | |
}, | |
getName: function() { | |
assert(this instanceof Model, 'this is not an instance of Model') | |
return this.name; | |
}, | |
getChildren: function() { | |
assert(this instanceof Model, 'this is not an instance of Model') | |
return this.children; | |
} | |
} | |
var model = new Model('parent', [new Model('child')]); | |
var options = { | |
locals: { | |
model: model | |
} | |
}; | |
assert.equal(model.getRoot().getChildren().length, 1); | |
assert.equal(model.getRoot().getChildren()[0].getName(), expected); | |
console.log('These pass'); | |
assert.equal(swig.render(workingTemplate, options), expected); | |
assert.equal(swig.compile(workingTemplate)(options.locals), expected); | |
console.log('This fails') | |
var rendered = swig.render(failingTemplate, options); | |
assert.equal(rendered, expected); | |
console.log('As does this'); | |
var compiled = swig.compile(failingTemplate); | |
var compiledRendered = compiled(options.locals) | |
assert.equal(compiledRendered, expected); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment