diff --git a/src/builtin/tags/include.ts b/src/builtin/tags/include.ts index 077cb81bed..9d5ad44f88 100644 --- a/src/builtin/tags/include.ts +++ b/src/builtin/tags/include.ts @@ -39,7 +39,7 @@ export default { if (this.with) { hash[filepath] = new Expression(this.with).evaluateSync(ctx) } - const templates = this.liquid.getTemplateSync(filepath, ctx.opts) + const templates = this.liquid.parseFileSync(filepath, ctx.opts) ctx.push(hash) this.liquid.renderer.renderTemplatesSync(templates, ctx, emitter) ctx.pop() @@ -69,7 +69,7 @@ export default { if (this.with) { hash[filepath] = await new Expression(this.with).evaluate(ctx) } - const templates = await this.liquid.getTemplate(filepath, ctx.opts) + const templates = await this.liquid.parseFile(filepath, ctx.opts) ctx.push(hash) await this.liquid.renderer.renderTemplates(templates, ctx, emitter) ctx.pop() diff --git a/src/builtin/tags/layout.ts b/src/builtin/tags/layout.ts index 68dacc34e4..99f3d6bc2f 100644 --- a/src/builtin/tags/layout.ts +++ b/src/builtin/tags/layout.ts @@ -39,8 +39,8 @@ export default { blocks[''] = html } const templates = ctx.sync - ? this.liquid.getTemplateSync(layout, ctx.opts) - : await this.liquid.getTemplate(layout, ctx.opts) + ? this.liquid.parseFileSync(layout, ctx.opts) + : await this.liquid.parseFile(layout, ctx.opts) ctx.push(hash) ctx.setRegister('blockMode', BlockMode.OUTPUT) const partial = ctx.sync diff --git a/src/liquid.ts b/src/liquid.ts index d3fe96c3fe..0d99ffc9f2 100644 --- a/src/liquid.ts +++ b/src/liquid.ts @@ -15,7 +15,7 @@ import { LiquidOptions, normalizeStringArray, NormalizedFullOptions, applyDefaul import { FilterImplOptions } from './template/filter/filter-impl-options' import IFS from './fs/ifs' -type GetTemplateResult = ITemplate[] | undefined +type nullableTemplates = ITemplate[] | null export * from './types' @@ -59,40 +59,52 @@ export class Liquid { const tpl = this.parse(html) return this.renderSync(tpl, scope, opts) } - public getTemplateSync (file: string, opts?: LiquidOptions): ITemplate[] { + public parseFileSync (file: string, opts?: LiquidOptions): ITemplate[] { const options = { ...this.options, ...normalize(opts) } const paths = options.root.map(root => this.fs.resolve(root, file, options.extname)) for (const filepath of paths) { const tpl = this.respectCache(filepath, () => { - if (!(this.fs.existsSync(filepath))) return + if (!(this.fs.existsSync(filepath))) return null return this.parse(this.fs.readFileSync(filepath), filepath) }) - if (tpl) return tpl + if (tpl !== null) return tpl } throw this.lookupError(file, options.root) } - public async getTemplate (file: string, opts?: LiquidOptions): Promise { + public async parseFile (file: string, opts?: LiquidOptions): Promise { const options = { ...this.options, ...normalize(opts) } const paths = options.root.map(root => this.fs.resolve(root, file, options.extname)) for (const filepath of paths) { const tpl = await this.respectCache(filepath, async () => { - if (!(await this.fs.exists(filepath))) return + if (!(await this.fs.exists(filepath))) return null return this.parse(await this.fs.readFile(filepath), filepath) }) - if (tpl !== undefined) return tpl + if (tpl !== null) return tpl } throw this.lookupError(file, options.root) } + /** + * @deprecated use parseFile instead + */ + public async getTemplate (file: string, opts?: LiquidOptions): Promise { + return this.parseFile(file, opts) + } + /** + * @deprecated use parseFileSync instead + */ + public getTemplateSync (file: string, opts?: LiquidOptions): ITemplate[] { + return this.parseFileSync(file, opts) + } public async renderFile (file: string, ctx?: object, opts?: LiquidOptions) { - const templates = await this.getTemplate(file, opts) + const templates = await this.parseFile(file, opts) return this.render(templates, ctx, opts) } public renderFileSync (file: string, ctx?: object, opts?: LiquidOptions) { const options = normalize(opts) - const templates = this.getTemplateSync(file, options) + const templates = this.parseFileSync(file, options) return this.renderSync(templates, ctx, opts) } public async evalValue (str: string, ctx: Context): Promise { @@ -127,21 +139,19 @@ export class Liquid { return err } - private setCache (filepath: string, tpl: T): T { - if (tpl === undefined) return tpl + private setCache (filepath: string, tpl: T): T { this.cache[filepath] = tpl return tpl } - private respectCache (filepath: string, resolver: () => GetTemplateResult): GetTemplateResult - private respectCache (filepath: string, resolver: () => Promise): Promise - private respectCache (filepath: string, resolver: () => GetTemplateResult | Promise): GetTemplateResult | Promise { + private respectCache (filepath: string, resolver: () => nullableTemplates): nullableTemplates + private respectCache (filepath: string, resolver: () => Promise): Promise + private respectCache (filepath: string, resolver: () => nullableTemplates | Promise): nullableTemplates | Promise { if (!this.options.cache) return resolver() if (this.cache[filepath]) return this.cache[filepath] + const tpl = resolver() - if (tpl instanceof Promise) { - return tpl.then(c => this.setCache(filepath, c)) - } - return this.setCache(filepath, tpl) + const setCacheIfDefined = (tpl: nullableTemplates) => tpl === null ? null : this.setCache(filepath, tpl) + return tpl instanceof Promise ? tpl.then(setCacheIfDefined) : setCacheIfDefined(tpl) } }