feature: support static filepath resolving by options.dynamicPartials,

fix #51
This commit is contained in:
harttle
2017-12-21 14:09:30 +08:00
parent 4cbfa6d204
commit ace271c7f0
7 changed files with 28 additions and 2 deletions
+2
View File
@@ -86,6 +86,8 @@ Defaults to `["."]`
* `cache` indicates whether or not to cache resolved templates. Defaults to `false`.
* `dynamicPartials`: if set, treat `<filepath>` parameter in `{%include filepath %}`, `{%layout filepath%}` as a variable, otherwise as a literal value. Defaults to `true`.
* `strict_filters` is used to enable strict filter existence. If set to `false`, undefined filters will be rendered as empty string. Otherwise, undefined filters will cause an exception. Defaults to `false`.
* `strict_variables` is used to enable strict variable derivation.
+1
View File
@@ -115,6 +115,7 @@ function factory (options) {
root: ['.'],
cache: false,
extname: '',
dynamicPartials: true,
trim_tag_right: false,
trim_tag_left: false,
trim_value_right: false,
+1
View File
@@ -172,6 +172,7 @@ function matchRightBracket (str, begin) {
exports.factory = function (ctx, opts) {
var defaultOptions = {
dynamicPartials: true,
strict_variables: false,
strict_filters: false,
blocks: {},
+4 -1
View File
@@ -16,7 +16,10 @@ module.exports = function (liquid) {
}
},
render: function (scope, hash) {
var filepath = Liquid.evalValue(this.value, scope)
var filepath = this.value
if (scope.opts.dynamicPartials) {
filepath = Liquid.evalValue(this.value, scope)
}
var originBlocks = scope.opts.blocks
var originBlockMode = scope.opts.blockMode
+1 -1
View File
@@ -19,7 +19,7 @@ module.exports = function (liquid) {
this.tpls = liquid.parser.parse(remainTokens)
},
render: function (scope, hash) {
var layout = Liquid.evalValue(this.layout, scope)
var layout = scope.opts.dynamicPartials ? Liquid.evalValue(this.layout, scope) : this.layout
// render the remaining tokens immediately
scope.opts.blockMode = 'store'
+10
View File
@@ -88,4 +88,14 @@ describe('tags/include', function () {
return expect(liquid.renderFile('personInfo.html', ctx)).to
.eventually.equal('This is a person <p>Joe Shmoe<br/>City: Dallas</p>')
})
it('should support static filename', function () {
var staticLiquid = new Liquid({dynamicPartials: false, root: '/'})
mock({
'/with.html': 'X{% include color.html shape: "rect" %}Y',
'/color.html': 'shape:{{shape}}'
})
return expect(staticLiquid.renderFile('with.html')).to
.eventually.equal('Xshape:rectY')
})
})
+9
View File
@@ -96,4 +96,13 @@ describe('tags/layout', function () {
return expect(liquid.renderFile('/main.html')).to
.eventually.equal('blackredA')
})
it('should support static filename', function () {
mock({
'/parent.html': '{{color}}{%block%}{%endblock%}',
'/main.html': '{% layout parent.html color:"black"%}{%block%}A{%endblock%}'
})
var staticLiquid = Liquid({ root: '/', dynamicPartials: false })
return expect(staticLiquid.renderFile('/main.html')).to
.eventually.equal('blackA')
})
})