mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 12:50:38 -07:00
fix: type compatible with v9 tag definition, support Context as scope in various render APIs, #570
This commit is contained in:
+17
-1
@@ -1,4 +1,4 @@
|
||||
import { Tokenizer, Context, Liquid, Drop, toValueSync } from '../..'
|
||||
import { TopLevelToken, TagToken, Tokenizer, Context, Liquid, Drop, toValueSync } from '../..'
|
||||
import { expect, use } from 'chai'
|
||||
import * as chaiAsPromised from 'chai-as-promised'
|
||||
import * as sinon from 'sinon'
|
||||
@@ -346,4 +346,20 @@ describe('Issues', function () {
|
||||
const html = await liquid.parseAndRender(tpl)
|
||||
expect(html).to.match(/^\s*This is a love or luck potion.\s+This is a strength or health or love potion.\s*$/)
|
||||
})
|
||||
it('#570 tag registration compatible to v9', async () => {
|
||||
const liquid = new Liquid()
|
||||
liquid.registerTag('metadata_file', {
|
||||
parse (tagToken: TagToken, remainTokens: TopLevelToken[]) {
|
||||
this.str = tagToken.args
|
||||
},
|
||||
async render (ctx: Context) {
|
||||
const content = await Promise.resolve(`{{${this.str}}}`)
|
||||
return this.liquid.parseAndRender(content.toString(), ctx)
|
||||
}
|
||||
})
|
||||
const tpl = '{% metadata_file foo %}'
|
||||
const ctx = { foo: 'FOO' }
|
||||
const html = await liquid.parseAndRender(tpl, ctx)
|
||||
expect(html).to.equal('FOO')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -57,6 +57,10 @@ describe('Liquid', function () {
|
||||
const html = await engine.parseAndRender(src, { foo: Promise.resolve('FOO') })
|
||||
expect(html).to.equal('FOO')
|
||||
})
|
||||
it('should parse and render with Context', async function () {
|
||||
const html = await engine.parseAndRender('{{foo}}', new Context({ foo: 'FOO' }))
|
||||
expect(html).to.equal('FOO')
|
||||
})
|
||||
})
|
||||
describe('#parseAndRenderSync', function () {
|
||||
const engine = new Liquid()
|
||||
@@ -145,6 +149,11 @@ describe('Liquid', function () {
|
||||
const str = await engine.evalValue('"foo"', ctx)
|
||||
expect(str).to.equal('foo')
|
||||
})
|
||||
it('should support plain scope', async function () {
|
||||
const engine = new Liquid()
|
||||
const str = await engine.evalValue('foo', { foo: 'FOO' })
|
||||
expect(str).to.equal('FOO')
|
||||
})
|
||||
})
|
||||
describe('#evalValueSync', function () {
|
||||
it('should eval string literal', function () {
|
||||
|
||||
Reference in New Issue
Block a user