Skip to content

Commit

Permalink
added feature to ignore missing template files for include
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathias Nestler committed Oct 21, 2015
1 parent 90e3913 commit 6a628ae
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ var Compiler = Object.extend({

this.emit('env.getTemplate(');
this._compileExpression(node.template, frame);
this.emitLine(', false, '+this._templateName()+', ' + this.makeCallback(id));
this.emitLine(', false, '+this._templateName()+', false, ' + this.makeCallback(id));
this.addScopeLevel();

this.emitLine(id + '.getExported(' +
Expand All @@ -919,7 +919,7 @@ var Compiler = Object.extend({

this.emit('env.getTemplate(');
this._compileExpression(node.template, frame);
this.emitLine(', false, '+this._templateName()+', ' + this.makeCallback(importedId));
this.emitLine(', false, '+this._templateName()+', false, ' + this.makeCallback(importedId));
this.addScopeLevel();

this.emitLine(importedId + '.getExported(' +
Expand Down Expand Up @@ -1002,7 +1002,7 @@ var Compiler = Object.extend({

this.emit('env.getTemplate(');
this._compileExpression(node.template, frame);
this.emitLine(', true, '+this._templateName()+', ' + this.makeCallback('_parentTemplate'));
this.emitLine(', true, '+this._templateName()+', false, ' + this.makeCallback('_parentTemplate'));

// extends is a dynamic tag and can occur within a block like
// `if`, so if this happens we need to capture the parent
Expand All @@ -1023,7 +1023,7 @@ var Compiler = Object.extend({

this.emit('env.getTemplate(');
this._compileExpression(node.template, frame);
this.emitLine(', false, '+this._templateName()+', '+ this.makeCallback(id));
this.emitLine(', false, '+this._templateName()+', ' + node.ignoreMissing + ', ' + this.makeCallback(id));
this.addScopeLevel();

this.emitLine(id + '.render(' +
Expand Down
23 changes: 18 additions & 5 deletions src/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ var Environment = Obj.extend({
return (isRelative && loader.resolve)? loader.resolve(parentName, filename) : filename;
},

getTemplate: function(name, eagerCompile, parentName, cb) {
getTemplate: function(name, eagerCompile, parentName, ignoreMissing, cb) {
var that = this;
var tmpl = null;
if(name && name.raw) {
Expand Down Expand Up @@ -193,10 +193,16 @@ var Environment = Obj.extend({
} else {
var syncResult;
var _this = this;
var fileMissing = false;

var createTemplate = function(err, info) {
if(!info && !err) {
err = new Error('template not found: ' + name);
if(!ignoreMissing) {
err = new Error('template not found: ' + name);
}
else {
fileMissing = true;
}
}

if (err) {
Expand All @@ -208,11 +214,18 @@ var Environment = Obj.extend({
}
}
else {
var tmpl = new Template(info.src, _this,
var tmpl;
if(ignoreMissing && fileMissing) {
tmpl = new Template('', _this,
'', eagerCompile);
}
else {
tmpl = new Template(info.src, _this,
info.path, eagerCompile);

if(!info.noCache) {
info.loader.cache[name] = tmpl;
if(!info.noCache) {
info.loader.cache[name] = tmpl;
}
}

if(cb) {
Expand Down
2 changes: 1 addition & 1 deletion src/nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ var Block = Node.extend('Block', { fields: ['name', 'body'] });
var Super = Node.extend('Super', { fields: ['blockName', 'symbol'] });
var TemplateRef = Node.extend('TemplateRef', { fields: ['template'] });
var Extends = TemplateRef.extend('Extends');
var Include = TemplateRef.extend('Include');
var Include = Node.extend('Include', { fields: ['template', 'ignoreMissing'] });
var Set = Node.extend('Set', { fields: ['targets', 'value'] });
var Output = NodeList.extend('Output');
var TemplateData = Literal.extend('TemplateData');
Expand Down
16 changes: 15 additions & 1 deletion src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,21 @@ var Parser = Object.extend({
},

parseInclude: function() {
return this.parseTemplateRef('include', nodes.Include);
var tagName = 'include';
var tag = this.peekToken();
if(!this.skipSymbol(tagName)) {
this.fail('parseInclude: expected '+ tagName);
}

var node = new nodes.Include(tag.lineno, tag.colno);
node.template = this.parseExpression();

if(this.skipSymbol('ignoreMissing')) {
node.ignoreMissing = true;
}

this.advanceAfterBlockEnd(tag.value);
return node;
},

parseIf: function() {
Expand Down
7 changes: 7 additions & 0 deletions tests/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,13 @@
{ name: 'thedude', data: {tmpl: 'include.html'} },
'hello world FooInclude thedude');

equal('hello world {% include "missing.html" ignoreMissing %}',
'hello world ');

equal('hello world {% include "missing.html" ignoreMissing %}',
{ name: 'thedude' },
'hello world ');

finish(done);
});

Expand Down

0 comments on commit 6a628ae

Please sign in to comment.