mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 04:10:40 -07:00
feat: when tag with multiple values
This commit is contained in:
@@ -12,7 +12,7 @@ Input
|
|||||||
{% case handle %}
|
{% case handle %}
|
||||||
{% when "cake" %}
|
{% when "cake" %}
|
||||||
This is a cake
|
This is a cake
|
||||||
{% when "cookie" %}
|
{% when "cookie", "biscuit" %}
|
||||||
This is a cookie
|
This is a cookie
|
||||||
{% else %}
|
{% else %}
|
||||||
This is not a cake nor a cookie
|
This is not a cake nor a cookie
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ title: case
|
|||||||
{% case handle %}
|
{% case handle %}
|
||||||
{% when "cake" %}
|
{% when "cake" %}
|
||||||
This is a cake
|
This is a cake
|
||||||
{% when "cookie" %}
|
{% when "cookie", "biscuit" %}
|
||||||
This is a cookie
|
This is a cookie
|
||||||
{% else %}
|
{% else %}
|
||||||
This is not a cake nor a cookie
|
This is not a cake nor a cookie
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { toValue, Value, Emitter, TagToken, TopLevelToken, Context, Template, TagImplOptions, ParseStream } from '../../types'
|
import { toValue, evalToken, Value, Emitter, TagToken, TopLevelToken, Context, Template, TagImplOptions, ParseStream } from '../../types'
|
||||||
|
import { Tokenizer } from '../../parser/tokenizer'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
parse: function (tagToken: TagToken, remainTokens: TopLevelToken[]) {
|
parse: function (tagToken: TagToken, remainTokens: TopLevelToken[]) {
|
||||||
@@ -9,10 +10,21 @@ export default {
|
|||||||
let p: Template[] = []
|
let p: Template[] = []
|
||||||
const stream: ParseStream = this.liquid.parser.parseStream(remainTokens)
|
const stream: ParseStream = this.liquid.parser.parseStream(remainTokens)
|
||||||
.on('tag:when', (token: TagToken) => {
|
.on('tag:when', (token: TagToken) => {
|
||||||
this.cases.push({
|
p = []
|
||||||
val: new Value(token.args, this.liquid),
|
|
||||||
templates: p = []
|
const tokenizer = new Tokenizer(token.args, this.liquid.options.operatorsTrie)
|
||||||
})
|
|
||||||
|
while (!tokenizer.end()) {
|
||||||
|
const value = tokenizer.readValue()
|
||||||
|
if (value) {
|
||||||
|
this.cases.push({
|
||||||
|
val: value,
|
||||||
|
templates: p
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
tokenizer.readTo(',')
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.on('tag:else', () => (p = this.elseTemplates))
|
.on('tag:else', () => (p = this.elseTemplates))
|
||||||
.on('tag:endcase', () => stream.stop())
|
.on('tag:endcase', () => stream.stop())
|
||||||
@@ -28,7 +40,7 @@ export default {
|
|||||||
const r = this.liquid.renderer
|
const r = this.liquid.renderer
|
||||||
const cond = toValue(yield this.cond.value(ctx, ctx.opts.lenientIf))
|
const cond = toValue(yield this.cond.value(ctx, ctx.opts.lenientIf))
|
||||||
for (const branch of this.cases) {
|
for (const branch of this.cases) {
|
||||||
const val = toValue(yield branch.val.value(ctx, ctx.opts.lenientIf))
|
const val = evalToken(branch.val, ctx, ctx.opts.lenientIf)
|
||||||
if (val === cond) {
|
if (val === cond) {
|
||||||
yield r.renderTemplates(branch.templates, ctx, emitter)
|
yield r.renderTemplates(branch.templates, ctx, emitter)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -64,4 +64,11 @@ describe('tags/case', function () {
|
|||||||
return expect(html).to.equal('d')
|
return expect(html).to.equal('d')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
it('should support case with multiple values', async function () {
|
||||||
|
const src = '{% case "b" %}' +
|
||||||
|
'{% when "a", "b" %}foo' +
|
||||||
|
'{%endcase%}'
|
||||||
|
const html = await liquid.parseAndRender(src)
|
||||||
|
return expect(html).to.equal('foo')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user