mirror of
https://github.com/harttle/liquidjs.git
synced 2026-10-03 00:55:13 -07:00
feature: template string for {%include%} argument, #72
This commit is contained in:
+27
-15
@@ -1,3 +1,4 @@
|
|||||||
|
'use strict'
|
||||||
const Liquid = require('..')
|
const Liquid = require('..')
|
||||||
const lexical = Liquid.lexical
|
const lexical = Liquid.lexical
|
||||||
const withRE = new RegExp(`with\\s+(${lexical.value.source})`)
|
const withRE = new RegExp(`with\\s+(${lexical.value.source})`)
|
||||||
@@ -7,7 +8,7 @@ const assert = require('../src/util/assert.js')
|
|||||||
module.exports = function (liquid) {
|
module.exports = function (liquid) {
|
||||||
liquid.registerTag('include', {
|
liquid.registerTag('include', {
|
||||||
parse: function (token) {
|
parse: function (token) {
|
||||||
var match = staticFileRE.exec(token.args)
|
let match = staticFileRE.exec(token.args)
|
||||||
if (match) {
|
if (match) {
|
||||||
this.staticValue = match[0]
|
this.staticValue = match[0]
|
||||||
}
|
}
|
||||||
@@ -23,21 +24,32 @@ module.exports = function (liquid) {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
render: function (scope, hash) {
|
render: function (scope, hash) {
|
||||||
var filepath = scope.opts.dynamicPartials
|
let pFilepath
|
||||||
? Liquid.evalValue(this.value, scope)
|
if (scope.opts.dynamicPartials) {
|
||||||
: this.staticValue
|
if (lexical.quotedLine.exec(this.value)) {
|
||||||
assert(filepath, `cannot include with empty filename`)
|
let template = this.value.slice(1, -1)
|
||||||
|
pFilepath = liquid.parseAndRender(template, scope.getAll(), scope.opts)
|
||||||
var originBlocks = scope.opts.blocks
|
} else {
|
||||||
var originBlockMode = scope.opts.blockMode
|
pFilepath = Promise.resolve(Liquid.evalValue(this.value, scope))
|
||||||
scope.opts.blocks = {}
|
}
|
||||||
scope.opts.blockMode = 'output'
|
} else {
|
||||||
|
pFilepath = Promise.resolve(this.staticValue)
|
||||||
if (this.with) {
|
|
||||||
hash[filepath] = Liquid.evalValue(this.with, scope)
|
|
||||||
}
|
}
|
||||||
return liquid.getTemplate(filepath, scope.opts.root)
|
|
||||||
.then((templates) => {
|
let originBlocks = scope.opts.blocks
|
||||||
|
let originBlockMode = scope.opts.blockMode
|
||||||
|
|
||||||
|
return pFilepath
|
||||||
|
.then(filepath => {
|
||||||
|
assert(filepath, `cannot include with empty filename`)
|
||||||
|
scope.opts.blocks = {}
|
||||||
|
scope.opts.blockMode = 'output'
|
||||||
|
if (this.with) {
|
||||||
|
hash[filepath] = Liquid.evalValue(this.with, scope)
|
||||||
|
}
|
||||||
|
return liquid.getTemplate(filepath, scope.opts.root)
|
||||||
|
})
|
||||||
|
.then(templates => {
|
||||||
scope.push(hash)
|
scope.push(hash)
|
||||||
return liquid.renderer.renderTemplates(templates, scope)
|
return liquid.renderer.renderTemplates(templates, scope)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -23,6 +23,14 @@ describe('tags/include', function () {
|
|||||||
return expect(liquid.renderFile('/current.html')).to
|
return expect(liquid.renderFile('/current.html')).to
|
||||||
.eventually.equal('barfoobar')
|
.eventually.equal('barfoobar')
|
||||||
})
|
})
|
||||||
|
it('should support template string', function () {
|
||||||
|
mock({
|
||||||
|
'/current.html': 'bar{% include "bar/{{name}}" %}bar',
|
||||||
|
'/bar/foo.html': 'foo'
|
||||||
|
})
|
||||||
|
return expect(liquid.renderFile('/current.html', {name: 'foo.html'})).to
|
||||||
|
.eventually.equal('barfoobar')
|
||||||
|
})
|
||||||
|
|
||||||
it('should throw when not specified', function () {
|
it('should throw when not specified', function () {
|
||||||
mock({
|
mock({
|
||||||
|
|||||||
Reference in New Issue
Block a user