Skip to content

Commit

Permalink
feat: add option for keeping variable type in output
Browse files Browse the repository at this point in the history
  • Loading branch information
Pixcell authored and harttle committed Dec 16, 2020
1 parent e84fbf3 commit cd92e77
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/builtin/tags/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default {
const r = this.liquid.renderer
const html = childDefined !== undefined
? childDefined
: yield r.renderTemplates(this.tpls, ctx)
: yield r.renderTemplates(this.tpls, ctx, new Emitter(ctx.opts.keepOutputType))

if (ctx.getRegister('blockMode', BlockMode.OUTPUT) === BlockMode.STORE) {
blocks[this.block] = html
Expand Down
3 changes: 2 additions & 1 deletion src/builtin/tags/capture.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Tokenizer, assert, Template, Context, TagImplOptions, TagToken, TopLevelToken } from '../../types'
import { evalQuotedToken } from '../../render/expression'
import { Emitter } from '../../render/emitter'

export default {
parse: function (tagToken: TagToken, remainTokens: TopLevelToken[]) {
Expand All @@ -19,7 +20,7 @@ export default {
},
render: function * (ctx: Context) {
const r = this.liquid.renderer
const html = yield r.renderTemplates(this.templates, ctx)
const html = yield r.renderTemplates(this.templates, ctx, new Emitter(ctx.opts.keepOutputType))
ctx.bottom()[this.variable] = html
}
} as TagImplOptions
Expand Down
2 changes: 1 addition & 1 deletion src/builtin/tags/include.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default {
const { renderer } = liquid
const filepath = ctx.opts.dynamicPartials
? (TypeGuards.isQuotedToken(file)
? yield renderer.renderTemplates(liquid.parse(evalQuotedToken(file)), ctx)
? yield renderer.renderTemplates(liquid.parse(evalQuotedToken(file)), ctx, new Emitter(ctx.opts.keepOutputType))
: yield evalToken(file, ctx))
: file.getText()
assert(filepath, () => `illegal filename "${file.getText()}":"${filepath}"`)
Expand Down
6 changes: 3 additions & 3 deletions src/builtin/tags/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ export default {
const { renderer } = liquid
const filepath = ctx.opts.dynamicPartials
? (TypeGuards.isQuotedToken(file)
? yield renderer.renderTemplates(liquid.parse(evalQuotedToken(file)), ctx)
? yield renderer.renderTemplates(liquid.parse(evalQuotedToken(file)), ctx, new Emitter(ctx.opts.keepOutputType))
: evalToken(this.file, ctx))
: file.getText()
assert(filepath, () => `illegal filename "${file.getText()}":"${filepath}"`)

// render the remaining tokens immediately
ctx.setRegister('blockMode', BlockMode.STORE)
const blocks = ctx.getRegister('blocks')
const html = yield renderer.renderTemplates(this.tpls, ctx)
const html = yield renderer.renderTemplates(this.tpls, ctx, new Emitter(ctx.opts.keepOutputType))
if (blocks[''] === undefined) blocks[''] = html
const templates = yield liquid._parseFile(filepath, ctx.opts, ctx.sync)
ctx.push(yield hash.render(ctx))
ctx.setRegister('blockMode', BlockMode.OUTPUT)
const partial = yield renderer.renderTemplates(templates, ctx)
const partial = yield renderer.renderTemplates(templates, ctx, new Emitter(ctx.opts.keepOutputType))
ctx.pop()
emitter.write(partial)
}
Expand Down
2 changes: 1 addition & 1 deletion src/builtin/tags/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default {
const { renderer } = liquid
const filepath = ctx.opts.dynamicPartials
? (TypeGuards.isQuotedToken(file)
? yield renderer.renderTemplates(liquid.parse(evalQuotedToken(file)), ctx)
? yield renderer.renderTemplates(liquid.parse(evalQuotedToken(file)), ctx, new Emitter(ctx.opts.keepOutputType))
: evalToken(file, ctx))
: file.getText()
assert(filepath, () => `illegal filename "${file.getText()}":"${filepath}"`)
Expand Down
6 changes: 5 additions & 1 deletion src/liquid-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export interface LiquidOptions {
fs?: FS;
/** the global environment passed down to all partial templates, i.e. templates included by `include`, `layout` and `render` tags. */
globals?: object;
/** Whether or not to keep value type when writing the Output. Defaults to `false`. */
keepOutputType?: boolean;
}

interface NormalizedOptions extends LiquidOptions {
Expand All @@ -69,6 +71,7 @@ export interface NormalizedFullOptions extends NormalizedOptions {
outputDelimiterRight: string;
greedy: boolean;
globals: object;
keepOutputType: boolean;
}

export const defaultOptions: NormalizedFullOptions = {
Expand All @@ -89,7 +92,8 @@ export const defaultOptions: NormalizedFullOptions = {
strictFilters: false,
strictVariables: false,
lenientIf: false,
globals: {}
globals: {},
keepOutputType: false
}

export function normalize (options?: LiquidOptions): NormalizedOptions {
Expand Down
16 changes: 9 additions & 7 deletions src/liquid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { LiquidOptions, normalizeStringArray, NormalizedFullOptions, applyDefaul
import { FilterImplOptions } from './template/filter/filter-impl-options'
import { FS } from './fs/fs'
import { toPromise, toValue } from './util/async'
import { Emitter } from './render/emitter'

export * from './types'

Expand Down Expand Up @@ -43,26 +44,27 @@ export class Liquid {
return this.parser.parse(tokens)
}

public _render (tpl: Template[], scope?: object, opts?: LiquidOptions, sync?: boolean): IterableIterator<string> {
public _render (tpl: Template[], scope?: object, opts?: LiquidOptions, sync?: boolean): IterableIterator<any> {
const options = { ...this.options, ...normalize(opts) }
const ctx = new Context(scope, options, sync)
return this.renderer.renderTemplates(tpl, ctx)
const emitter = new Emitter(options.keepOutputType)
return this.renderer.renderTemplates(tpl, ctx, emitter)
}
public async render (tpl: Template[], scope?: object, opts?: LiquidOptions): Promise<string> {
public async render (tpl: Template[], scope?: object, opts?: LiquidOptions): Promise<any> {
return toPromise(this._render(tpl, scope, opts, false))
}
public renderSync (tpl: Template[], scope?: object, opts?: LiquidOptions): string {
public renderSync (tpl: Template[], scope?: object, opts?: LiquidOptions): any {
return toValue(this._render(tpl, scope, opts, true))
}

public _parseAndRender (html: string, scope?: object, opts?: LiquidOptions, sync?: boolean): IterableIterator<string> {
public _parseAndRender (html: string, scope?: object, opts?: LiquidOptions, sync?: boolean): IterableIterator<any> {
const tpl = this.parse(html)
return this._render(tpl, scope, opts, sync)
}
public async parseAndRender (html: string, scope?: object, opts?: LiquidOptions): Promise<string> {
public async parseAndRender (html: string, scope?: object, opts?: LiquidOptions): Promise<any> {
return toPromise(this._parseAndRender(html, scope, opts, false))
}
public parseAndRenderSync (html: string, scope?: object, opts?: LiquidOptions): string {
public parseAndRenderSync (html: string, scope?: object, opts?: LiquidOptions): any {
return toValue(this._parseAndRender(html, scope, opts, true))
}

Expand Down
15 changes: 12 additions & 3 deletions src/render/emitter.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
export class Emitter {
public html = '';
public html: any = '';
public break = false;
public continue = false;
private keepOutputType? = false;

public write (html: string) {
this.html += html
constructor (keepOutputType: boolean|undefined) {
this.keepOutputType = keepOutputType
}

public write (html: any) {
if (this.keepOutputType && typeof html !== 'string') {
this.html = html
} else {
this.html += html as string
}
}
}
2 changes: 1 addition & 1 deletion src/render/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Template } from '../template/template'
import { Emitter } from './emitter'

export class Render {
public * renderTemplates (templates: Template[], ctx: Context, emitter = new Emitter()): IterableIterator<string> {
public * renderTemplates (templates: Template[], ctx: Context, emitter: Emitter): IterableIterator<any> {
for (const tpl of templates) {
try {
const html = yield tpl.render(ctx, emitter)
Expand Down
6 changes: 5 additions & 1 deletion src/template/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export class Output extends TemplateImpl<OutputToken> implements Template {
}
public * render (ctx: Context, emitter: Emitter) {
const val = yield this.value.value(ctx)
emitter.write(stringify(toValue(val)))
if (ctx.opts.keepOutputType) {
emitter.write(toValue(val))
} else {
emitter.write(stringify(toValue(val)))
}
}
}
3 changes: 2 additions & 1 deletion test/unit/render/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Context } from '../../../src/context/context'
import { HTMLToken } from '../../../src/tokens/html-token'
import { Render } from '../../../src/render/render'
import { HTML } from '../../../src/template/html'
import { Emitter } from '../../../src/render/emitter'
import { toThenable } from '../../../src/util/async'

describe('render', function () {
Expand All @@ -15,7 +16,7 @@ describe('render', function () {
it('should render html', async function () {
const scope = new Context()
const token = { getContent: () => '<p>' } as HTMLToken
const html = await toThenable(render.renderTemplates([new HTML(token)], scope))
const html = await toThenable(render.renderTemplates([new HTML(token)], scope, new Emitter(scope.opts.keepOutputType)))
return expect(html).to.equal('<p>')
})
})
Expand Down

0 comments on commit cd92e77

Please sign in to comment.