mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 12:50:38 -07:00
refactor: switch Context <-> Scope concepts
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { evalValue } from '../../render/syntax'
|
||||
import Scope from '../../scope/scope'
|
||||
import Context from '../../context/context'
|
||||
import { isArray } from '../../util/underscore'
|
||||
import { FilterImpl } from './filter-impl'
|
||||
|
||||
@@ -19,11 +19,11 @@ export class Filter {
|
||||
this.impl = impl || (x => x)
|
||||
this.args = args
|
||||
}
|
||||
async render (value: any, scope: Scope) {
|
||||
async render (value: any, ctx: Context) {
|
||||
const argv: any[] = []
|
||||
for (const arg of this.args) {
|
||||
if (isArray(arg)) argv.push([arg[0], await evalValue(arg[1], scope)])
|
||||
else argv.push(await evalValue(arg, scope))
|
||||
if (isArray(arg)) argv.push([arg[0], await evalValue(arg[1], ctx)])
|
||||
else argv.push(await evalValue(arg, ctx))
|
||||
}
|
||||
return this.impl.apply(null, [value, ...argv])
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import Scope from '../scope/scope'
|
||||
import Context from '../context/context'
|
||||
import Token from '../parser/token'
|
||||
|
||||
export default interface ITemplate {
|
||||
token: Token;
|
||||
render(scope: Scope): Promise<string>;
|
||||
render(ctx: Context): Promise<string>;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ import Value from './value'
|
||||
import { stringify } from '../util/underscore'
|
||||
import Template from '../template/template'
|
||||
import ITemplate from '../template/itemplate'
|
||||
import Scope from '../scope/scope'
|
||||
import Context from '../context/context'
|
||||
import OutputToken from '../parser/output-token'
|
||||
|
||||
export default class Output extends Template<OutputToken> implements ITemplate {
|
||||
@@ -11,8 +11,8 @@ export default class Output extends Template<OutputToken> implements ITemplate {
|
||||
super(token)
|
||||
this.value = new Value(token.value, strictFilters)
|
||||
}
|
||||
async render (scope: Scope): Promise<string> {
|
||||
const html = await this.value.value(scope)
|
||||
async render (ctx: Context): Promise<string> {
|
||||
const html = await this.value.value(ctx)
|
||||
return stringify(html)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { hashCapture } from '../../parser/lexical'
|
||||
import { evalValue } from '../../render/syntax'
|
||||
import Scope from '../../scope/scope'
|
||||
import Context from '../../context/context'
|
||||
|
||||
/**
|
||||
* Key-Value Pairs Representing Tag Arguments
|
||||
@@ -10,14 +10,14 @@ import Scope from '../../scope/scope'
|
||||
*/
|
||||
export default class Hash {
|
||||
[key: string]: any
|
||||
static async create (markup: string, scope: Scope) {
|
||||
static async create (markup: string, ctx: Context) {
|
||||
const instance = new Hash()
|
||||
let match
|
||||
hashCapture.lastIndex = 0
|
||||
while ((match = hashCapture.exec(markup))) {
|
||||
const k = match[1]
|
||||
const v = match[2]
|
||||
instance[k] = await evalValue(v, scope)
|
||||
instance[k] = await evalValue(v, ctx)
|
||||
}
|
||||
return instance
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import Scope from '../../scope/scope'
|
||||
import Context from '../../context/context'
|
||||
import TagToken from '../../parser/tag-token'
|
||||
import Token from '../../parser/token'
|
||||
import Hash from '../../template/tag/hash'
|
||||
@@ -6,5 +6,5 @@ import ITagImpl from './itag-impl'
|
||||
|
||||
export default interface ITagImplOptions {
|
||||
parse?: (this: ITagImpl, token: TagToken, remainingTokens: Array<Token>) => void
|
||||
render?: (this: ITagImpl, scope: Scope, hash: Hash) => any | Promise<any>
|
||||
render?: (this: ITagImpl, ctx: Context, hash: Hash) => any | Promise<any>
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { create, stringify } from '../../util/underscore'
|
||||
import assert from '../../util/assert'
|
||||
import Scope from '../../scope/scope'
|
||||
import Context from '../../context/context'
|
||||
import ITagImpl from './itag-impl'
|
||||
import ITagImplOptions from './itag-impl-options'
|
||||
import Liquid from '../../liquid'
|
||||
@@ -27,13 +27,13 @@ export default class Tag extends Template<TagToken> implements ITemplate {
|
||||
this.impl.parse(token, tokens)
|
||||
}
|
||||
}
|
||||
async render (scope: Scope) {
|
||||
const hash = await Hash.create(this.token.args, scope)
|
||||
async render (ctx: Context) {
|
||||
const hash = await Hash.create(this.token.args, ctx)
|
||||
const impl = this.impl
|
||||
if (typeof impl.render !== 'function') {
|
||||
return ''
|
||||
}
|
||||
const html = await impl.render(scope, hash)
|
||||
const html = await impl.render(ctx, hash)
|
||||
return stringify(html)
|
||||
}
|
||||
static register (name: string, tag: ITagImplOptions) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { evalExp } from '../render/syntax'
|
||||
import { FilterArgs, Filter } from './filter/filter'
|
||||
import Scope from '../scope/scope'
|
||||
import Context from '../context/context'
|
||||
|
||||
export default class Value {
|
||||
private strictFilters: boolean
|
||||
@@ -47,10 +47,10 @@ export default class Value {
|
||||
}
|
||||
this.filters.push(new Filter(name, args, this.strictFilters))
|
||||
}
|
||||
async value (scope: Scope) {
|
||||
let val = await evalExp(this.initial, scope)
|
||||
async value (ctx: Context) {
|
||||
let val = await evalExp(this.initial, ctx)
|
||||
for (const filter of this.filters) {
|
||||
val = await filter.render(val, scope)
|
||||
val = await filter.render(val, ctx)
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user