Skip to content

Commit ace271c

Browse files
committed
feature: support static filepath resolving by options.dynamicPartials,
fix #51
1 parent 4cbfa6d commit ace271c

File tree

7 files changed

+28
-2
lines changed

7 files changed

+28
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ Defaults to `["."]`
8686

8787
* `cache` indicates whether or not to cache resolved templates. Defaults to `false`.
8888

89+
* `dynamicPartials`: if set, treat `<filepath>` parameter in `{%include filepath %}`, `{%layout filepath%}` as a variable, otherwise as a literal value. Defaults to `true`.
90+
8991
* `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`.
9092

9193
* `strict_variables` is used to enable strict variable derivation.

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ function factory (options) {
115115
root: ['.'],
116116
cache: false,
117117
extname: '',
118+
dynamicPartials: true,
118119
trim_tag_right: false,
119120
trim_tag_left: false,
120121
trim_value_right: false,

src/scope.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ function matchRightBracket (str, begin) {
172172

173173
exports.factory = function (ctx, opts) {
174174
var defaultOptions = {
175+
dynamicPartials: true,
175176
strict_variables: false,
176177
strict_filters: false,
177178
blocks: {},

tags/include.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ module.exports = function (liquid) {
1616
}
1717
},
1818
render: function (scope, hash) {
19-
var filepath = Liquid.evalValue(this.value, scope)
19+
var filepath = this.value
20+
if (scope.opts.dynamicPartials) {
21+
filepath = Liquid.evalValue(this.value, scope)
22+
}
2023

2124
var originBlocks = scope.opts.blocks
2225
var originBlockMode = scope.opts.blockMode

tags/layout.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = function (liquid) {
1919
this.tpls = liquid.parser.parse(remainTokens)
2020
},
2121
render: function (scope, hash) {
22-
var layout = Liquid.evalValue(this.layout, scope)
22+
var layout = scope.opts.dynamicPartials ? Liquid.evalValue(this.layout, scope) : this.layout
2323

2424
// render the remaining tokens immediately
2525
scope.opts.blockMode = 'store'

test/tags/include.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,14 @@ describe('tags/include', function () {
8888
return expect(liquid.renderFile('personInfo.html', ctx)).to
8989
.eventually.equal('This is a person <p>Joe Shmoe<br/>City: Dallas</p>')
9090
})
91+
92+
it('should support static filename', function () {
93+
var staticLiquid = new Liquid({dynamicPartials: false, root: '/'})
94+
mock({
95+
'/with.html': 'X{% include color.html shape: "rect" %}Y',
96+
'/color.html': 'shape:{{shape}}'
97+
})
98+
return expect(staticLiquid.renderFile('with.html')).to
99+
.eventually.equal('Xshape:rectY')
100+
})
91101
})

test/tags/layout.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,13 @@ describe('tags/layout', function () {
9696
return expect(liquid.renderFile('/main.html')).to
9797
.eventually.equal('blackredA')
9898
})
99+
it('should support static filename', function () {
100+
mock({
101+
'/parent.html': '{{color}}{%block%}{%endblock%}',
102+
'/main.html': '{% layout parent.html color:"black"%}{%block%}A{%endblock%}'
103+
})
104+
var staticLiquid = Liquid({ root: '/', dynamicPartials: false })
105+
return expect(staticLiquid.renderFile('/main.html')).to
106+
.eventually.equal('blackA')
107+
})
99108
})

0 commit comments

Comments
 (0)