diff --git a/src/builtin/tags/block.ts b/src/builtin/tags/block.ts index 9c0e6cd37..9c78eac4a 100644 --- a/src/builtin/tags/block.ts +++ b/src/builtin/tags/block.ts @@ -1,10 +1,5 @@ import BlockMode from '../../context/block-mode' -import { TagToken } from '../../parser/tag-token' -import { Token } from '../../parser/token' -import { ITemplate } from '../../template/itemplate' -import { Context } from '../../context/context' -import { ITagImplOptions } from '../../template/tag/itag-impl-options' -import { ParseStream } from '../../parser/parse-stream' +import { ParseStream, TagToken, Token, ITemplate, Context, ITagImplOptions, Emitter, Hash } from '../../types' export default { parse: function (token: TagToken, remainTokens: Token[]) { @@ -19,7 +14,7 @@ export default { }) stream.start() }, - render: async function (ctx: Context) { + render: async function (ctx: Context, hash: Hash, emitter: Emitter) { const blocks = ctx.getRegister('blocks') const childDefined = blocks[this.block] const html = childDefined !== undefined @@ -28,8 +23,8 @@ export default { if (ctx.getRegister('blockMode', BlockMode.OUTPUT) === BlockMode.STORE) { blocks[this.block] = html - return '' + return } - return html + emitter.write(html) } } as ITagImplOptions diff --git a/src/builtin/tags/cycle.ts b/src/builtin/tags/cycle.ts index 088f880cc..0af3518b5 100644 --- a/src/builtin/tags/cycle.ts +++ b/src/builtin/tags/cycle.ts @@ -1,9 +1,6 @@ import { assert } from '../../util/assert' import { value as rValue } from '../../parser/lexical' -import { Expression } from '../../render/expression' -import { TagToken } from '../../parser/tag-token' -import { Context } from '../../context/context' -import { ITagImplOptions } from '../../template/tag/itag-impl-options' +import { Emitter, Expression, TagToken, Context, ITagImplOptions, Hash } from '../../types' const groupRE = new RegExp(`^(?:(${rValue.source})\\s*:\\s*)?(.*)$`) const candidatesRE = new RegExp(rValue.source, 'g') @@ -24,7 +21,7 @@ export default { assert(this.candidates.length, `empty candidates: ${tagToken.raw}`) }, - render: async function (ctx: Context) { + render: async function (ctx: Context, hash: Hash, emitter: Emitter) { const group = this.group.value(ctx) const fingerprint = `cycle:${group}:` + this.candidates.join(',') const groups = ctx.getRegister('cycle') @@ -37,7 +34,6 @@ export default { const candidate = this.candidates[idx] idx = (idx + 1) % this.candidates.length groups[fingerprint] = idx - - return new Expression(candidate).value(ctx) + emitter.write(new Expression(candidate).value(ctx)) } } as ITagImplOptions diff --git a/src/builtin/tags/decrement.ts b/src/builtin/tags/decrement.ts index 314810d76..6a7f15c51 100644 --- a/src/builtin/tags/decrement.ts +++ b/src/builtin/tags/decrement.ts @@ -1,9 +1,7 @@ import { assert } from '../../util/assert' import { identifier } from '../../parser/lexical' -import { TagToken } from '../../parser/tag-token' -import { Context } from '../../context/context' -import { ITagImplOptions } from '../../template/tag/itag-impl-options' -import { isNumber } from '../../util/underscore' +import { Emitter, TagToken, Context, ITagImplOptions, Hash } from '../../types' +import { isNumber, stringify } from '../../util/underscore' export default { parse: function (token: TagToken) { @@ -11,11 +9,11 @@ export default { assert(match, `illegal identifier ${token.args}`) this.variable = match[0] }, - render: function (context: Context) { + render: function (context: Context, hash: Hash, emitter: Emitter) { const scope = context.environments if (!isNumber(scope[this.variable])) { scope[this.variable] = 0 } - return --scope[this.variable] + emitter.write(stringify(--scope[this.variable])) } } as ITagImplOptions diff --git a/src/builtin/tags/for.ts b/src/builtin/tags/for.ts index 33728c381..116a75dfe 100644 --- a/src/builtin/tags/for.ts +++ b/src/builtin/tags/for.ts @@ -47,7 +47,8 @@ export default { } } if (!isArray(collection) || !collection.length) { - return this.liquid.renderer.renderTemplates(this.elseTemplates, ctx) + this.liquid.renderer.renderTemplates(this.elseTemplates, ctx, emitter) + return } const offset = hash.offset || 0 diff --git a/src/builtin/tags/increment.ts b/src/builtin/tags/increment.ts index 078428ca7..18ca13fbc 100644 --- a/src/builtin/tags/increment.ts +++ b/src/builtin/tags/increment.ts @@ -1,21 +1,21 @@ import { assert } from '../../util/assert' import { identifier } from '../../parser/lexical' -import { isNumber } from '../../util/underscore' -import { ITagImplOptions } from '../../template/tag/itag-impl-options' +import { isNumber, stringify } from '../../util/underscore' +import { Emitter, TagToken, Context, ITagImplOptions, Hash } from '../../types' export default { - parse: function (token) { + parse: function (token: TagToken) { const match = token.args.match(identifier) assert(match, `illegal identifier ${token.args}`) this.variable = match![0] }, - render: function (context) { + render: function (context: Context, hash: Hash, emitter: Emitter) { const scope = context.environments if (!isNumber(scope[this.variable])) { scope[this.variable] = 0 } const val = scope[this.variable] scope[this.variable]++ - return val + emitter.write(stringify(val)) } } as ITagImplOptions diff --git a/src/builtin/tags/layout.ts b/src/builtin/tags/layout.ts index 120ee450c..017b8f0ea 100644 --- a/src/builtin/tags/layout.ts +++ b/src/builtin/tags/layout.ts @@ -1,8 +1,7 @@ import { assert } from '../../util/assert' import { value as rValue } from '../../parser/lexical' -import { Expression, TagToken, Token, Context, ITagImplOptions } from '../../types' +import { Emitter, Hash, Expression, TagToken, Token, Context, ITagImplOptions } from '../../types' import BlockMode from '../../context/block-mode' -import { Hash } from '../../template/tag/hash' const staticFileRE = /\S+/ @@ -20,7 +19,7 @@ export default { this.tpls = this.liquid.parser.parse(remainTokens) }, - render: async function (ctx: Context, hash: Hash) { + render: async function (ctx: Context, hash: Hash, emitter: Emitter) { const layout = ctx.opts.dynamicPartials ? await (new Expression(this.layout).value(ctx)) : this.staticLayout @@ -38,6 +37,6 @@ export default { ctx.setRegister('blockMode', BlockMode.OUTPUT) const partial = await this.liquid.renderer.renderTemplates(templates, ctx) ctx.pop() - return partial + emitter.write(partial) } } as ITagImplOptions diff --git a/src/builtin/tags/raw.ts b/src/builtin/tags/raw.ts index 791dfbe2e..5390f7236 100644 --- a/src/builtin/tags/raw.ts +++ b/src/builtin/tags/raw.ts @@ -1,4 +1,4 @@ -import { TagToken, Token, ITagImplOptions } from '../../types' +import { TagToken, Token, ITagImplOptions, Context, Emitter, Hash } from '../../types' export default { parse: function (tagToken: TagToken, remainTokens: Token[]) { @@ -15,7 +15,7 @@ export default { }) stream.start() }, - render: function () { - return this.tokens.map((token: Token) => token.raw).join('') + render: function (ctx: Context, hash: Hash, emitter: Emitter) { + emitter.write(this.tokens.map((token: Token) => token.raw).join('')) } } as ITagImplOptions diff --git a/src/template/tag/tag.ts b/src/template/tag/tag.ts index faced874d..320b59f34 100644 --- a/src/template/tag/tag.ts +++ b/src/template/tag/tag.ts @@ -1,4 +1,4 @@ -import { stringify, isFunction } from '../../util/underscore' +import { isFunction } from '../../util/underscore' import { assert } from '../../util/assert' import { Liquid } from '../../liquid' import { Template } from '../../template/template' @@ -26,8 +26,7 @@ export class Tag extends Template implements ITemplate { public async render (ctx: Context, emitter: Emitter) { const hash = await Hash.create(this.token.args, ctx) const impl = this.impl - const html = isFunction(impl.render) ? stringify(await impl.render(ctx, hash, emitter)) : '' - html && emitter.write(html) + if (isFunction(impl.render)) await impl.render(ctx, hash, emitter) } public static register (name: string, tag: ITagImplOptions) { Tag.impls[name] = tag