Permalink
Browse files

`include` tags should accept context variables

closes gh-7
  • Loading branch information...
1 parent 969019c commit 395e02cec021c6e7c95e6c0aa4041f3013f7455e @paularmstrong committed Sep 27, 2011
Showing with 21 additions and 9 deletions.
  1. +1 −9 lib/tags.js
  2. +20 −0 tests/tags.test.js
View
@@ -33,18 +33,10 @@ exports.include = function (indent) {
// Circular includes are VERBOTTEN. This will crash the server.
return [
'(function () {',
- ' if (' + helpers.check(template) + ') {',
- ' var __template = ' + helpers.escape(template) + ';',
- ' }',
- ' else if (' + helpers.check(template, '__context') + ') {',
- ' var __template = ' + helpers.escape(template, '__context') + ';',
- ' }',
+ helpers.setVar('__template', parser.parseVariable(template)),
' if (typeof __template === "string") {',
' __output += __this.fromFile(__template).render(__context, __parents);',
' }',
- ' else if (typeof __template === "object" && __template.render) {',
- ' __output += __template.render(__context, __parents);',
- ' }',
'})();'
].join('\n' + indent);
};
View
@@ -1,4 +1,5 @@
var testCase = require('nodeunit').testCase,
+ util = require('util'),
swig = require('../index');
exports['custom tags'] = function (test) {
@@ -18,6 +19,25 @@ exports['custom tags'] = function (test) {
test.done();
};
+exports.include = testCase({
+ setUp: function (callback) {
+ swig.init({ root: __dirname + '/templates' });
+ callback();
+ },
+
+ basic: function (test) {
+ var tmpl8 = swig.fromString('{% include "included_2.html" %}');
+ test.strictEqual(tmpl8.render({ array: ['foo'] }), '1');
+ test.done();
+ },
+
+ variable: function (test) {
+ var tmpl8 = swig.fromString('{% include inc %}');
+ test.strictEqual(tmpl8.render({ inc: 'included_2.html', array: ['foo'] }), '1');
+ test.done();
+ }
+});
+
exports.if = testCase({
setUp: function (callback) {
swig.init({});

0 comments on commit 395e02c

Please sign in to comment.