mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-14 20:00:39 -07:00
feat: support function calls, closes #222
This commit is contained in:
@@ -70,8 +70,8 @@ export class Context {
|
||||
export function readProperty (obj: Scope, key: string) {
|
||||
if (isNil(obj)) return obj
|
||||
obj = toLiquid(obj)
|
||||
if (isFunction(obj[key])) return obj[key]()
|
||||
if (obj instanceof Drop) {
|
||||
if (isFunction(obj[key])) return obj[key]()
|
||||
if (obj.hasOwnProperty(key)) return obj[key]
|
||||
return obj.liquidMethodMissing(key)
|
||||
}
|
||||
|
||||
@@ -70,4 +70,12 @@ describe('Issues', function () {
|
||||
const html = await engine.parseAndRender(`{{ '{{' }}{{ '}}' }}`)
|
||||
expect(html).to.equal('{{}}')
|
||||
})
|
||||
it('#222 Support function calls', async () => {
|
||||
const engine = new Liquid()
|
||||
const html = await engine.parseAndRender(
|
||||
`{{ obj.property }}`,
|
||||
{ obj: { property: () => 'BAR' } }
|
||||
)
|
||||
expect(html).to.equal('BAR')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -9,9 +9,6 @@ describe('filters/html', function () {
|
||||
it('should escape normal string', function () {
|
||||
return test('{{ "Tetsuro Takara" | escape }}', 'Tetsuro Takara')
|
||||
})
|
||||
it('should escape function', function () {
|
||||
return test('{{ func | escape }}', { func: function () {} }, 'function () { }')
|
||||
})
|
||||
it('should escape undefined', function () {
|
||||
return test('{{ nonExistent.value | escape }}', '')
|
||||
})
|
||||
|
||||
@@ -15,6 +15,8 @@ describe('Context', function () {
|
||||
first: 'f',
|
||||
last: 'l'
|
||||
},
|
||||
func: () => 'FUNC',
|
||||
objFunc: () => ({ prop: 'PROP' }),
|
||||
bar: {
|
||||
zoo: 'coo',
|
||||
'Mr.Smith': 'John',
|
||||
@@ -59,6 +61,12 @@ describe('Context', function () {
|
||||
it('should read .last of array', async function () {
|
||||
expect(ctx.get(['bar', 'arr', 'last'])).to.equal('b')
|
||||
})
|
||||
it('should call function', async function () {
|
||||
expect(ctx.get(['func'])).to.equal('FUNC')
|
||||
})
|
||||
it('should call function before read nested property', async function () {
|
||||
expect(ctx.get(['objFunc', 'prop'])).to.equal('PROP')
|
||||
})
|
||||
})
|
||||
|
||||
describe('#getFromScope()', function () {
|
||||
|
||||
Reference in New Issue
Block a user