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