diff --git a/tags/include.js b/tags/include.js index a02ba8c61..e9e2c58cc 100644 --- a/tags/include.js +++ b/tags/include.js @@ -1,42 +1,40 @@ -const Liquid = require('..'); -const lexical = Liquid.lexical; -const withRE = new RegExp(`with\\s+(${lexical.value.source})`); -const assert = require('../src/util/assert.js'); +const Liquid = require('..') +const lexical = Liquid.lexical +const withRE = new RegExp(`with\\s+(${lexical.value.source})`) +const assert = require('../src/util/assert.js') -module.exports = function(liquid) { +module.exports = function (liquid) { + liquid.registerTag('include', { + parse: function (token) { + var match = lexical.value.exec(token.args) + assert(match, `illegal token ${token.raw}`) + this.value = match[0] - liquid.registerTag('include', { - parse: function(token){ - var match = lexical.value.exec(token.args); - assert(match, `illegal token ${token.raw}`); - this.value = match[0]; + match = withRE.exec(token.args) + if (match) { + this.with = match[1] + } + }, + render: function (scope, hash) { + var filepath = Liquid.evalValue(this.value, scope) - match = withRE.exec(token.args); - if(match){ - this.with = match[1]; - } - }, - render: function(scope, hash) { - var filepath = Liquid.evalValue(this.value, scope); + var register = scope.get('liquid') + var originBlocks = register.blocks + register.blocks = {} - var register = scope.get('liquid'); - var originBlocks = register.blocks; - register.blocks = {}; - - if(this.with){ - hash[filepath] = Liquid.evalValue(this.with, scope); - } - return liquid.getTemplate(filepath, register.root) - .then((templates) => { - scope.push(hash); - return liquid.renderer.renderTemplates(templates, scope); - }) - .then((html) => { - scope.pop(); - register.blocks = originBlocks; - return html; - }); - } - }); - -}; + if (this.with) { + hash[filepath] = Liquid.evalValue(this.with, scope) + } + return liquid.getTemplate(filepath, register.root) + .then((templates) => { + scope.push(hash) + return liquid.renderer.renderTemplates(templates, scope) + }) + .then((html) => { + scope.pop() + register.blocks = originBlocks + return html + }) + } + }) +} diff --git a/tags/layout.js b/tags/layout.js index bbf384b69..f53e424e2 100644 --- a/tags/layout.js +++ b/tags/layout.js @@ -1,66 +1,68 @@ -const Liquid = require('..'); -const Promise = require('any-promise'); -const lexical = Liquid.lexical; -const assert = require('../src/util/assert.js'); +const Liquid = require('..') +const Promise = require('any-promise') +const lexical = Liquid.lexical +const assert = require('../src/util/assert.js') -module.exports = function(liquid) { +module.exports = function (liquid) { + liquid.registerTag('layout', { + parse: function (token, remainTokens) { + var match = lexical.value.exec(token.args) + assert(match, `illegal token ${token.raw}`) - liquid.registerTag('layout', { - parse: function(token, remainTokens){ - var match = lexical.value.exec(token.args); - assert(match, `illegal token ${token.raw}`); + this.layout = match[0] + this.tpls = liquid.parser.parse(remainTokens) + }, + render: function (scope, hash) { + var layout = Liquid.evalValue(this.layout, scope) + var register = scope.get('liquid') - this.layout = match[0]; - this.tpls = liquid.parser.parse(remainTokens); - }, - render: function(scope, hash) { - var layout = Liquid.evalValue(this.layout, scope); - var register = scope.get('liquid'); + // render the remaining tokens immediately + return liquid.renderer.renderTemplates(this.tpls, scope) + // now register.blocks contains rendered blocks + .then(() => liquid.getTemplate(layout, register.root)) + .then(templates => { + // push the hash + scope.push(hash) + // render the parent + return liquid.renderer.renderTemplates(templates, scope) + }) + // pop the hash + .then(partial => { + scope.pop() + return partial + }) + } + }) - // render the remaining tokens immediately - return liquid.renderer.renderTemplates(this.tpls, scope) - // now register.blocks contains rendered blocks - .then(() => liquid.getTemplate(layout, register.root)) - // push the hash - .then(templates => (scope.push(hash), templates)) - // render the parent - .then(templates => liquid.renderer.renderTemplates(templates, scope)) - // pop the hash - .then(partial => (scope.pop(), partial)); - } - }); + liquid.registerTag('block', { + parse: function (token, remainTokens) { + var match = /\w+/.exec(token.args) + this.block = match ? match[0] : 'anonymous' - liquid.registerTag('block', { - parse: function(token, remainTokens){ - var match = /\w+/.exec(token.args); - this.block = match ? match[0] : 'anonymous'; - - this.tpls = []; - var stream = liquid.parser.parseStream(remainTokens) - .on('tag:endblock', () => stream.stop()) - .on('template', tpl => this.tpls.push(tpl)) - .on('end', () => { - throw new Error(`tag ${token.raw} not closed`); - }); - stream.start(); - }, - render: function(scope){ - var register = scope.get('liquid'); - var html = register.blocks[this.block]; - // if not defined yet - if (html === undefined) { - return liquid.renderer.renderTemplates(this.tpls, scope) - .then((partial) => { - register.blocks[this.block] = partial; - return partial; - }); - } - // if already defined by desendents - else { - register.blocks[this.block] = html; - return Promise.resolve(html); - } - } - }); - -}; + this.tpls = [] + var stream = liquid.parser.parseStream(remainTokens) + .on('tag:endblock', () => stream.stop()) + .on('template', tpl => this.tpls.push(tpl)) + .on('end', () => { + throw new Error(`tag ${token.raw} not closed`) + }) + stream.start() + }, + render: function (scope) { + var register = scope.get('liquid') + var html = register.blocks[this.block] + // if not defined yet + if (html === undefined) { + return liquid.renderer.renderTemplates(this.tpls, scope) + .then((partial) => { + register.blocks[this.block] = partial + return partial + }) + } else { + // if already defined by desendents + register.blocks[this.block] = html + return Promise.resolve(html) + } + } + }) +}