feat: pass context to filters

This commit is contained in:
harttle
2019-04-17 10:24:55 +08:00
parent 6fb4796202
commit 00bc1efe6a
15 changed files with 77 additions and 85 deletions
+1 -3
View File
@@ -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}
}
+1 -1
View File
@@ -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
+4 -3
View File
@@ -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
+1 -1
View File
@@ -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
+1 -1
View File
@@ -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) {
+7 -7
View File
@@ -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
}
}
+6 -5
View File
@@ -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
+1 -1
View File
@@ -59,7 +59,7 @@ export default {
html += '</td>'
}
if (collection.length) html += '</tr>'
ctx.pop(scope)
ctx.pop()
return html
}
} as ITagImplOptions
+25 -25
View File
@@ -3,20 +3,23 @@ import { Drop } from '../drop/drop'
import { __assign } from 'tslib'
import assert from '../util/assert'
import { NormalizedFullOptions, applyDefault } from '../liquid-options'
import BlockMode from './block-mode'
import { Scope } from './scope'
export default class Context {
opts: NormalizedFullOptions
scopes: Array<Scope> = [{}]
environments: Scope
blocks: object = {}
groups: {[key: string]: number} = {}
blockMode: BlockMode = BlockMode.OUTPUT
private scopes: Array<Scope> = [{}]
private registers = {}
constructor (ctx: object = {}, opts?: NormalizedFullOptions) {
this.opts = applyDefault(opts)
this.environments = ctx
}
getRegister(key: string, defaultValue = {}) {
return this.registers[key] = this.registers[key] || defaultValue
}
setRegister(key: string, value: any) {
return this.registers[key] = value
}
getAll () {
return [this.environments, ...this.scopes]
.reduce((ctx, val) => __assign(ctx, val), {})
@@ -25,7 +28,7 @@ export default class Context {
const paths = await this.parseProp(path)
let ctx = this.findScope(paths[0]) || this.environments
for (const path of paths) {
ctx = this.readProperty(ctx, path)
ctx = readProperty(ctx, path)
if (_.isNil(ctx) && this.opts.strictVariables) {
throw new TypeError(`undefined variable: ${path}`)
}
@@ -35,15 +38,11 @@ export default class Context {
push (ctx: object) {
return this.scopes.push(ctx)
}
pop (ctx?: object): object | undefined {
if (!arguments.length) {
return this.scopes.pop()
}
const i = this.scopes.findIndex(scope => scope === ctx)
if (i === -1) {
throw new TypeError('scope not found, cannot pop')
}
return this.scopes.splice(i, 1)[0]
pop () {
return this.scopes.pop()
}
front () {
return this.scopes[0]
}
private findScope (key: string) {
for (let i = this.scopes.length - 1; i >= 0; i--) {
@@ -54,16 +53,6 @@ export default class Context {
}
return null
}
private readProperty (obj: Scope, key: string) {
if (_.isNil(obj)) return obj
obj = _.toLiquid(obj)
if (obj instanceof Drop) {
if (_.isFunction(obj[key])) return obj[key]()
if (obj.hasOwnProperty(key)) return obj[key]
return obj.liquidMethodMissing(key)
}
return key === 'size' ? readSize(obj) : obj[key]
}
/*
* Parse property access sequence from access string
@@ -124,6 +113,17 @@ export default class Context {
}
}
function readProperty (obj: Scope, key: string) {
if (_.isNil(obj)) return obj
obj = _.toLiquid(obj)
if (obj instanceof Drop) {
if (_.isFunction(obj[key])) return obj[key]()
if (obj.hasOwnProperty(key)) return obj[key]
return obj.liquidMethodMissing(key)
}
return key === 'size' ? readSize(obj) : obj[key]
}
function readSize (obj: Scope) {
if (!_.isNil(obj['size'])) return obj['size']
if (_.isArray(obj) || _.isString(obj)) return obj.length
+2 -2
View File
@@ -14,7 +14,7 @@ import { isTruthy, isFalsy, evalExp, evalValue } from './render/syntax'
import builtinTags from './builtin/tags'
import builtinFilters from './builtin/filters'
import { LiquidOptions, NormalizedFullOptions, applyDefault, normalize } from './liquid-options'
import { FilterImpl } from './template/filter/filter-impl'
import { FilterImplOptions } from './template/filter/filter-impl-options'
export default class Liquid {
public options: NormalizedFullOptions
@@ -72,7 +72,7 @@ export default class Liquid {
evalValue (str: string, ctx: Context) {
return new Value(str, this.options.strictFilters).value(ctx)
}
registerFilter (name: string, filter: FilterImpl) {
registerFilter (name: string, filter: FilterImplOptions) {
return Filter.register(name, filter)
}
registerTag (name: string, tag: ITagImplOptions) {
@@ -0,0 +1,3 @@
import { FilterImpl } from "./filter-impl";
export type FilterImplOptions = (this: FilterImpl, value: any, ...args: any[]) => any
+5 -1
View File
@@ -1 +1,5 @@
export type FilterImpl = (value: any, ...args: any[]) => any
import Context from "../../context/context";
export interface FilterImpl {
context: Context
}
+8 -8
View File
@@ -1,15 +1,15 @@
import { evalValue } from '../../render/syntax'
import Context from '../../context/context'
import { isArray } from '../../util/underscore'
import { FilterImpl } from './filter-impl'
import { FilterImplOptions } from './filter-impl-options'
export type FilterArgs = Array<string|[string?, string?]>
export class Filter {
name: string
impl: FilterImpl
impl: FilterImplOptions
args: FilterArgs
private static impls: {[key: string]: FilterImpl} = {}
private static impls: {[key: string]: FilterImplOptions} = {}
constructor (name: string, args: FilterArgs, strictFilters: boolean) {
const impl = Filter.impls[name]
@@ -19,15 +19,15 @@ export class Filter {
this.impl = impl || (x => x)
this.args = args
}
async render (value: any, ctx: Context) {
async render (value: any, context: Context) {
const argv: any[] = []
for (const arg of this.args) {
if (isArray(arg)) argv.push([arg[0], await evalValue(arg[1], ctx)])
else argv.push(await evalValue(arg, ctx))
if (isArray(arg)) argv.push([arg[0], await evalValue(arg[1], context)])
else argv.push(await evalValue(arg, context))
}
return this.impl.apply(null, [value, ...argv])
return this.impl.apply({ context }, [value, ...argv])
}
static register (name: string, filter: FilterImpl) {
static register (name: string, filter: FilterImplOptions) {
Filter.impls[name] = filter
}
static clear () {