Skip to content
This repository has been archived by the owner on Apr 11, 2018. It is now read-only.

Commit

Permalink
Added ignore missing tokens to include tag
Browse files Browse the repository at this point in the history
Allow includes to pass over missing includes and not throw an error.
  • Loading branch information
paularmstrong committed Feb 18, 2012
1 parent 88b494e commit 0e6704b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
6 changes: 5 additions & 1 deletion docs/tags.md
Expand Up @@ -36,11 +36,15 @@ See [Template inheritance](inheritance.md) for more information.

### include <a name="include" href="#include">#</a>

Includes a template in it's place. The template is rendered within the current context. Does not use and {% endinclude %}.
Includes a template in it's place. The template is rendered within the current context.

{% include template_path %}
{% include "path/to/template.js" %}

You can mark an include to `ignore missing` so that if the template does not exist, it will be ignored and no error will be thrown.

{% include "foobar.html" ignore missing %}

Locally declared context variables are _not_ passed to the included template by default. For example, in the following situations, your `inc.html` will not know about the variables `foo` nor `bar`:

{% set foo = "bar" %}
Expand Down
35 changes: 25 additions & 10 deletions lib/tags/include.js
Expand Up @@ -8,6 +8,8 @@ module.exports = function (indent, parentBlock, parser) {
var args = _.clone(this.args),
template = args.shift(),
context = '_context',
ignore = false,
out = '',
ctx;

indent = indent || '';
Expand All @@ -22,6 +24,12 @@ module.exports = function (indent, parentBlock, parser) {
args.pop();
}

if (args.length > 1 && args[0] === 'ignore' & args[1] === 'missing') {
args.shift();
args.shift();
ignore = true;
}

if (args.length && args[0] !== 'with') {
throw new Error('Invalid arguments passed to \'include\' tag.');
}
Expand All @@ -38,15 +46,22 @@ module.exports = function (indent, parentBlock, parser) {
}
}

out = '(function () {\n' +
helpers.setVar('__template', parser.parseVariable(template)) + '\n' +
' var includeContext = ' + context + ';\n';

if (ignore) {
out += 'try {\n';
}

out += ' if (typeof __template === "string") {\n';
out += ' _output += _this.compileFile(__template).render(includeContext, _parents);\n';
out += ' }\n';

if (ignore) {
out += '} catch (e) {}\n';
}
out += '})();\n';

// Circular includes are VERBOTTEN. This will crash the server.
return [
'(function () {',
helpers.setVar('__template', parser.parseVariable(template)),
' var includeContext = ' + context + ';',
' if (typeof __template === "string") {',
' _output += _this.compileFile(__template).render(includeContext, _parents);',
' }',
'})();'
].join('\n' + indent);
return out;
};
6 changes: 6 additions & 0 deletions lib/tags/include.test.js
Expand Up @@ -61,5 +61,11 @@ exports.include = testCase({
var tpl = swig.compile('{% set foo = { bar: "baz" } %}{% set bar = "hi" %}{% include "withcontext" with foo only %}');
test.strictEqual(tpl(), 'baz');
test.done();
},

'ignore missing': function (test) {
// does not throw
swig.compile('{% include "foobarbazbop" ignore missing %}')();
test.done();
}
});

0 comments on commit 0e6704b

Please sign in to comment.