Skip to content

Commit

Permalink
Changed include tag with context
Browse files Browse the repository at this point in the history
When adding context to an include tag, the context should always be an object. This makes swig more like Twig: http://twig.sensiolabs.org/doc/tags/include.html

closes paularmstronggh-55
  • Loading branch information
paularmstrong committed Feb 18, 2012
1 parent bda4621 commit 88b494e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
6 changes: 3 additions & 3 deletions docs/tags.md
Expand Up @@ -50,13 +50,13 @@ Locally declared context variables are _not_ passed to the included template by
{% include "inc.html" %}
{% endfor %}

In order to have your included template get these locally declared variables, you can use the `with` argument, followed by space-separated tokens of the local variables to include in the context:
In order to have your included template get these locally declared variables, you can use the `with` argument, followed by an object that will create the context of the included template:

{% set foo = "bar" %}
{% set foo = { bar: "baz" } %}
{% include "inc.html" with foo %}

{% for bar in thing %}
{% include "inc.html" with foo bar %}
{% include "inc.html" with bar %}
{% endfor %}

You can also use the `only` argument to restrict the context to the variables that you explicitly define.
Expand Down
11 changes: 5 additions & 6 deletions lib/tags/include.js
Expand Up @@ -7,7 +7,8 @@ var helpers = require('../helpers'),
module.exports = function (indent, parentBlock, parser) {
var args = _.clone(this.args),
template = args.shift(),
context = '_context';
context = '_context',
ctx;

indent = indent || '';

Expand All @@ -31,11 +32,9 @@ module.exports = function (indent, parentBlock, parser) {
throw new Error('Context for \'include\' tag not provided, but expected after \'with\' token.');
}

context = '_.extend(' + context + ', { ';
_.each(args, function (value, key) {
context += '"' + value + '": _context["' + value + '"] || ' + value + ',';
});
context += ' })';
ctx = args.shift();

context = '_context["' + ctx + '"] || ' + ctx;
}
}

Expand Down
16 changes: 8 additions & 8 deletions lib/tags/include.test.js
Expand Up @@ -3,7 +3,10 @@ var testCase = require('nodeunit').testCase,

exports.include = testCase({
setUp: function (callback) {
swig.init({ root: __dirname + '/../../tests/templates' });
swig.init({
root: __dirname + '/../../tests/templates',
allowErrors: true
});
callback();
},

Expand Down Expand Up @@ -37,11 +40,8 @@ exports.include = testCase({

'with context': function (test) {
swig.compile('{{ foo }}{{ bar }}', { filename: 'withcontext' });
var tpl = swig.compile('{% set foo = "1" %}{% include "withcontext" with foo %}');
test.strictEqual(tpl({}), '1');

tpl = swig.compile('{% set foo = "1" %}{% set bar = "2" %}{% include "withcontext" with foo bar %}');
test.strictEqual(tpl({}), '12');
var tpl = swig.compile('{% set foo = { bar: "baz" } %}{% include "withcontext" with foo %}');
test.strictEqual(tpl({}), 'baz');

test.throws(function () {
swig.compile('{% include "withcontext" with %}');
Expand All @@ -58,8 +58,8 @@ exports.include = testCase({

'with only': function (test) {
swig.compile('{{ foo }}{{ bar }}', { filename: 'withcontext' });
var tpl = swig.compile('{% set foo = "1" %}{% include "withcontext" with foo only %}');
test.strictEqual(tpl({}), '1');
var tpl = swig.compile('{% set foo = { bar: "baz" } %}{% set bar = "hi" %}{% include "withcontext" with foo only %}');
test.strictEqual(tpl(), 'baz');
test.done();
}
});

0 comments on commit 88b494e

Please sign in to comment.