diff --git a/tags/include.js b/tags/include.js index 5f944b4d9..e0d4a3814 100644 --- a/tags/include.js +++ b/tags/include.js @@ -1,14 +1,21 @@ const Liquid = require('..') const lexical = Liquid.lexical const withRE = new RegExp(`with\\s+(${lexical.value.source})`) +const staticFileRE = /\S+/ const assert = require('../src/util/assert.js') 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] + var match = staticFileRE.exec(token.args) + if (match) { + this.staticValue = match[0] + } + + match = lexical.value.exec(token.args) + if (match) { + this.value = match[0] + } match = withRE.exec(token.args) if (match) { @@ -16,10 +23,10 @@ module.exports = function (liquid) { } }, render: function (scope, hash) { - var filepath = this.value - if (scope.opts.dynamicPartials) { - filepath = Liquid.evalValue(this.value, scope) - } + var filepath = scope.opts.dynamicPartials + ? Liquid.evalValue(this.value, scope) + : this.staticValue + assert(filepath, `cannot include with empty filename`) var originBlocks = scope.opts.blocks var originBlockMode = scope.opts.blockMode diff --git a/tags/layout.js b/tags/layout.js index 21c8efc0b..b7c76d74f 100644 --- a/tags/layout.js +++ b/tags/layout.js @@ -2,6 +2,7 @@ const Liquid = require('..') const Promise = require('any-promise') const lexical = Liquid.lexical const assert = require('../src/util/assert.js') +const staticFileRE = /\S+/ /* * blockMode: @@ -12,14 +13,21 @@ const assert = require('../src/util/assert.js') module.exports = function (liquid) { liquid.registerTag('layout', { parse: function (token, remainTokens) { - var match = lexical.value.exec(token.args) - assert(match, `illegal token ${token.raw}`) + var match = staticFileRE.exec(token.args) + if (match) { + this.staticLayout = match[0] + } + + match = lexical.value.exec(token.args) + if (match) { + this.layout = match[0] + } - this.layout = match[0] this.tpls = liquid.parser.parse(remainTokens) }, render: function (scope, hash) { - var layout = scope.opts.dynamicPartials ? Liquid.evalValue(this.layout, scope) : this.layout + var layout = scope.opts.dynamicPartials ? Liquid.evalValue(this.layout, scope) : this.staticLayout + assert(layout, `cannot apply layout with empty filename`) // render the remaining tokens immediately scope.opts.blockMode = 'store' diff --git a/test/tags/include.js b/test/tags/include.js index a7a2206e3..9f0d8d1c0 100644 --- a/test/tags/include.js +++ b/test/tags/include.js @@ -24,13 +24,13 @@ describe('tags/include', function () { .eventually.equal('barfoobar') }) - it('should throw when illegal', function () { + it('should throw when not exist', function () { mock({ - '/illegal.html': '{%include%}' + '/parent.html': '{%include not-exist%}' }) - return liquid.renderFile('/illegal.html').catch(function (e) { - expect(e.name).to.equal('ParseError') - expect(e.message).to.match(/illegal token {%include%}/) + return liquid.renderFile('/parent.html').catch(function (e) { + expect(e.name).to.equal('RenderError') + expect(e.message).to.match(/cannot include with empty filename/) }) }) @@ -89,13 +89,35 @@ describe('tags/include', function () { .eventually.equal('This is a person
Joe Shmoe
City: Dallas