refactor: merge render register into scope

This commit is contained in:
harttle
2016-11-06 15:22:29 +08:00
parent 258f4a2073
commit d7b7b1724f
8 changed files with 21 additions and 30 deletions
+1 -9
View File
@@ -38,15 +38,7 @@ var _engine = {
return this.parser.parse(tokens);
},
render: function(tpl, ctx, opts) {
opts = _.assign({
strict_variables: false,
strict_filters: false,
root: []
}, opts);
this.renderer.initRegister(opts);
var scope = Scope.factory(ctx, {
strict: opts.strict_variables,
});
var scope = Scope.factory(ctx, opts);
return this.renderer.renderTemplates(tpl, scope);
},
parseAndRender: function(html, ctx, opts) {
+2 -7
View File
@@ -42,7 +42,7 @@ var render = {
if (template.name === 'break') {
return Promise.reject(new RenderBreak('break'));
}
return template.render(scope, this.register);
return template.render(scope);
},
evalOutput: function(template, scope) {
@@ -50,7 +50,7 @@ var render = {
var val = Syntax.evalExp(template.initial, scope);
template.filters.some(filter => {
if (filter.error) {
if (this.register.strict_filters) {
if (scope.get('liquid.strict_filters')) {
throw filter.error;
} else {
val = ''
@@ -60,16 +60,11 @@ var render = {
val = filter.render(val, scope);
});
return val;
},
initRegister: function(opts) {
return this.register = opts;
}
};
function factory() {
var instance = Object.create(render);
instance.register = {};
return instance;
}
+6 -3
View File
@@ -26,7 +26,7 @@ var Scope = {
},
get: function(str) {
var val = this.safeGet(str);
if (val === undefined && this.opts.strict) {
if (val === undefined && this.opts.strict_variables) {
throw new Error(`[strict_variables] undefined variable: ${str}`);
}
return val;
@@ -149,9 +149,12 @@ function matchRightBracket(str, begin) {
exports.factory = function(ctx, opts) {
opts = _.assign({
strict: false,
blocks: {}
strict_variables: false,
strict_filters: false,
blocks: {},
root: []
}, opts);
ctx = _.assign(ctx, {
liquid: opts
});
+2 -2
View File
@@ -18,9 +18,9 @@ module.exports = function() {
var tagImpls = {};
var _tagInstance = {
render: function(scope, register) {
render: function(scope) {
var obj = hash(this.token.args, scope);
return this.tagImpl.render && this.tagImpl.render(scope, obj, register) || Promise.resolve('');
return this.tagImpl.render && this.tagImpl.render(scope, obj) || Promise.resolve('');
},
parse: function(token, tokens){
this.type = 'tag';
+2 -1
View File
@@ -24,9 +24,10 @@ module.exports = function(liquid) {
assert(this.candidates.length, `empty candidates: ${tagToken.raw}`);
},
render: function(scope, hash, register) {
render: function(scope, hash) {
var group = Liquid.evalValue(this.group, scope);
var fingerprint = `cycle:${group}:` + this.candidates.join(',');
var register = scope.get('liquid');
var idx = register[fingerprint];
if(idx === undefined){
+5 -5
View File
@@ -16,12 +16,12 @@ module.exports = function(liquid) {
this.with = match[1];
}
},
render: function(scope, hash, register) {
render: function(scope, hash) {
var filepath = Liquid.evalValue(this.value, scope);
var reg = scope.get('liquid');
var originBlocks = reg.blocks;
reg.blocks = {};
var register = scope.get('liquid');
var originBlocks = register.blocks;
register.blocks = {};
if(this.with){
hash[filepath] = Liquid.evalValue(this.with, scope);
@@ -33,7 +33,7 @@ module.exports = function(liquid) {
})
.then((html) => {
scope.pop();
reg.blocks = originBlocks;
register.blocks = originBlocks;
return html;
});
}
+2 -2
View File
@@ -13,9 +13,9 @@ module.exports = function(liquid) {
this.layout = match[0];
this.tpls = liquid.parser.parse(remainTokens);
},
render: function(scope, hash, register) {
render: function(scope, hash) {
var layout = Liquid.evalValue(this.layout, scope);
var reg = scope.get('liquid');
var register = scope.get('liquid');
// render the remaining tokens immediately
return liquid.renderer.renderTemplates(this.tpls, scope)
+1 -1
View File
@@ -73,7 +73,7 @@ describe('scope', function() {
it('should throw undefined in strict mode', function() {
scope = Scope.factory(ctx, {
strict: true
strict_variables: true
});
function fn() {