mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 12:50:38 -07:00
feat: pass context to filters
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
import { FilterImpl } from '../../template/filter/filter-impl'
|
||||
|
||||
export default {
|
||||
'append': (v: string, arg: string) => v + arg,
|
||||
'prepend': (v: string, arg: string) => arg + v,
|
||||
@@ -27,4 +25,4 @@ export default {
|
||||
if (arr.length >= l) ret += o
|
||||
return ret
|
||||
}
|
||||
} as {[key: string]: FilterImpl}
|
||||
}
|
||||
@@ -14,6 +14,6 @@ export default {
|
||||
this.value = match[2]
|
||||
},
|
||||
render: async function (ctx: Context) {
|
||||
ctx.scopes[0][this.key] = await this.liquid.evalValue(this.value, ctx)
|
||||
ctx.front()[this.key] = await this.liquid.evalValue(this.value, ctx)
|
||||
}
|
||||
} as ITagImplOptions
|
||||
|
||||
@@ -20,13 +20,14 @@ export default {
|
||||
stream.start()
|
||||
},
|
||||
render: async function (ctx: Context) {
|
||||
const childDefined = ctx.blocks[this.block]
|
||||
const blocks = ctx.getRegister('blocks')
|
||||
const childDefined = blocks[this.block]
|
||||
const html = childDefined !== undefined
|
||||
? childDefined
|
||||
: await this.liquid.renderer.renderTemplates(this.tpls, ctx)
|
||||
|
||||
if (ctx.blockMode === BlockMode.STORE) {
|
||||
ctx.blocks[this.block] = html
|
||||
if (ctx.getRegister('blockMode', BlockMode.OUTPUT) === BlockMode.STORE) {
|
||||
blocks[this.block] = html
|
||||
return ''
|
||||
}
|
||||
return html
|
||||
|
||||
@@ -25,6 +25,6 @@ export default {
|
||||
},
|
||||
render: async function (ctx: Context) {
|
||||
const html = await this.liquid.renderer.renderTemplates(this.templates, ctx)
|
||||
ctx.scopes[0][this.variable] = html
|
||||
ctx.front()[this.variable] = html
|
||||
}
|
||||
} as ITagImplOptions
|
||||
|
||||
@@ -27,7 +27,7 @@ export default <ITagImplOptions>{
|
||||
render: async function (ctx: Context) {
|
||||
const group = await evalValue(this.group, ctx)
|
||||
const fingerprint = `cycle:${group}:` + this.candidates.join(',')
|
||||
const groups = ctx.groups
|
||||
const groups = ctx.getRegister('cycle')
|
||||
let idx = groups[fingerprint]
|
||||
|
||||
if (idx === undefined) {
|
||||
|
||||
@@ -41,20 +41,20 @@ export default <ITagImplOptions>{
|
||||
}
|
||||
assert(filepath, `cannot include with empty filename`)
|
||||
|
||||
const originBlocks = ctx.blocks
|
||||
const originBlockMode = ctx.blockMode
|
||||
const originBlocks = ctx.getRegister('blocks')
|
||||
const originBlockMode = ctx.getRegister('blockMode')
|
||||
|
||||
ctx.blocks = {}
|
||||
ctx.blockMode = BlockMode.OUTPUT
|
||||
ctx.setRegister('blocks', {})
|
||||
ctx.setRegister('blockMode', BlockMode.OUTPUT)
|
||||
if (this.with) {
|
||||
hash[filepath] = await evalValue(this.with, ctx)
|
||||
}
|
||||
const templates = await this.liquid.getTemplate(filepath, ctx.opts)
|
||||
ctx.push(hash)
|
||||
const html = await this.liquid.renderer.renderTemplates(templates, ctx)
|
||||
ctx.pop(hash)
|
||||
ctx.blocks = originBlocks
|
||||
ctx.blockMode = originBlockMode
|
||||
ctx.pop()
|
||||
ctx.setRegister('blocks', originBlocks)
|
||||
ctx.setRegister('blockMode', originBlockMode)
|
||||
return html
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,16 +31,17 @@ export default {
|
||||
assert(layout, `cannot apply layout with empty filename`)
|
||||
|
||||
// render the remaining tokens immediately
|
||||
ctx.blockMode = BlockMode.STORE
|
||||
ctx.setRegister('blockMode', BlockMode.STORE)
|
||||
const blocks = ctx.getRegister('blocks')
|
||||
const html = await this.liquid.renderer.renderTemplates(this.tpls, ctx)
|
||||
if (ctx.blocks[''] === undefined) {
|
||||
ctx.blocks[''] = html
|
||||
if (blocks[''] === undefined) {
|
||||
blocks[''] = html
|
||||
}
|
||||
const templates = await this.liquid.getTemplate(layout, ctx.opts)
|
||||
ctx.push(hash)
|
||||
ctx.blockMode = BlockMode.OUTPUT
|
||||
ctx.setRegister('blockMode', BlockMode.OUTPUT)
|
||||
const partial = await this.liquid.renderer.renderTemplates(templates, ctx)
|
||||
ctx.pop(hash)
|
||||
ctx.pop()
|
||||
return partial
|
||||
}
|
||||
} as ITagImplOptions
|
||||
|
||||
@@ -59,7 +59,7 @@ export default {
|
||||
html += '</td>'
|
||||
}
|
||||
if (collection.length) html += '</tr>'
|
||||
ctx.pop(scope)
|
||||
ctx.pop()
|
||||
return html
|
||||
}
|
||||
} as ITagImplOptions
|
||||
|
||||
Reference in New Issue
Block a user