enable cache: parse and render

This commit is contained in:
harttle
2016-06-20 00:34:54 +08:00
parent aa1df95714
commit 54bc206d36
16 changed files with 182 additions and 118 deletions
+25 -20
View File
@@ -2,36 +2,32 @@ const error = require('./error.js');
const Exp = require('./expression.js');
const assert = require('assert');
function stringify(val) {
if (typeof val === 'string') return val;
return JSON.stringify(val);
}
var render = {
function factory(Filter, Tag) {
function renderTemplates(templates, scope) {
renderTemplates: function(templates, scope) {
assert(scope, 'unable to evalTemplates: scope undefined');
var html = '', partial;
var html = '',
partial;
templates.some(template => {
if (scope.get('forloop.skip')) return true;
switch (template.type) {
case 'tag':
partial = renderTag(template, scope, this.register);
if(partial === undefined) return true;
partial = this.renderTag(template, scope, this.register);
if (partial === undefined) return true;
html += partial;
break;
case 'html':
html += template.value;
break;
case 'output':
var val = evalOutput(template, scope);
var val = this.evalOutput(template, scope);
html += stringify(val);
}
});
return html;
}
},
function renderTag(template, scope, register) {
renderTag: function(template, scope, register) {
if (template.name === 'continue') {
scope.set('forloop.skip', true);
return;
@@ -42,20 +38,29 @@ function factory(Filter, Tag) {
return;
}
return template.render(scope, register);
}
},
function evalOutput(template, scope) {
evalOutput: function(template, scope) {
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);
}
},
return {
renderTemplates, evalOutput, renderTag
};
resetRegisters: function(){
return this.register = {};
}
};
function factory() {
var instance = Object.create(render);
instance.register = {};
return instance;
}
factory.stringify = stringify;
function stringify(val) {
if (typeof val === 'string') return val;
return JSON.stringify(val);
}
module.exports = factory;