feature: strict rendering, close #9

This commit is contained in:
harttle
2016-09-26 23:25:08 +08:00
parent be6c67a1ba
commit 937d213a2d
8 changed files with 101 additions and 68 deletions
+58 -49
View File
@@ -9,7 +9,6 @@ var render = {
renderTemplates: function(templates, scope, opts) {
assert(scope, 'unable to evalTemplates: scope undefined');
opts = _.defaults(opts, {
strict_variables: false,
strict_filters: false
});
@@ -20,52 +19,52 @@ var render = {
// emptyPromise.then(renderTag(template0).then(renderTag(template1).then(renderTag(template2)...
var lastPromise = templates.reduce((promise, template) => {
return promise.then((partial) => {
if (scope.safeGet('forloop.skip')) {
return Promise.resolve('');
}
if (scope.safeGet('forloop.stop')) {
throw new Error('forloop.stop'); // this will stop/break the sequential promise chain and go to the catch
}
if (scope.safeGet('forloop.skip')) {
return Promise.resolve('');
}
if (scope.safeGet('forloop.stop')) {
throw new Error('forloop.stop'); // this will stop/break the sequential promise chain and go to the catch
}
var promiseLink = Promise.resolve('');
switch (template.type) {
case 'tag':
// Add Promises to the chain
promiseLink = this.renderTag(template, scope, this.register)
.then((partial) => {
if (partial === undefined) {
return true; // basically a noop (do nothing)
}
return html += partial;
});
break;
case 'html':
promiseLink = Promise.resolve(template.value)
.then((partial) => {
return html += partial;
});
break;
case 'output':
var val = this.evalOutput(template, scope);
promiseLink = Promise.resolve(val === undefined ? '' : stringify(val))
.then((partial) => {
return html += partial;
});
break;
}
var promiseLink = Promise.resolve('');
switch (template.type) {
case 'tag':
// Add Promises to the chain
promiseLink = this.renderTag(template, scope, this.register)
.then((partial) => {
if (partial === undefined) {
return true; // basically a noop (do nothing)
}
return html += partial;
});
break;
case 'html':
promiseLink = Promise.resolve(template.value)
.then((partial) => {
return html += partial;
});
break;
case 'output':
var val = this.evalOutput(template, scope, opts);
promiseLink = Promise.resolve(val === undefined ? '' : stringify(val))
.then((partial) => {
return html += partial;
});
break;
}
return promiseLink;
})
.catch((error) => {
if (error.message === 'forloop.skip') {
// the error is a controlled, purposeful stop. so just return the html that we have up to this point
return html;
} else {
// rethrow actual error
throw error;
}
});
}, Promise.resolve('')); // start the reduce chain with a resolved Promise. After first run, the "promise" argument
return promiseLink;
})
.catch((error) => {
if (error.message === 'forloop.skip') {
// the error is a controlled, purposeful stop. so just return the html that we have up to this point
return html;
} else {
// rethrow actual error
throw error;
}
});
}, Promise.resolve('')); // start the reduce chain with a resolved Promise. After first run, the "promise" argument
// in our reduce callback will be the returned promise from our "then" above. In this
// case, that's the promise returned from this.renderTag or a resolved promise with raw html.
@@ -92,14 +91,24 @@ var render = {
return template.render(scope, register);
},
evalOutput: function(template, scope) {
evalOutput: function(template, scope, opts) {
assert(scope, 'unable to evalOutput: scope undefined');
var val = Exp.evalExp(template.initial, scope);
return template.filters
.reduce((v, filter) => filter.render(v, scope), val);
template.filters.some(filter => {
if (filter.error) {
if (opts.strict_filters) {
throw filter.error;
} else { // render as null
val = '';
return true;
}
}
val = filter.render(val, scope);
});
return val;
},
resetRegisters: function(){
resetRegisters: function() {
return this.register = {};
}
};