Skip to content

Commit

Permalink
feat: alias getTemplate() to parseFile()
Browse files Browse the repository at this point in the history
* deprecate Liquid#getTemplate: use Liquid#parseFile instead
* deprecate Liquid#getTemplateSync: use Liquid#parseFileSync instead
  • Loading branch information
harttle committed Oct 7, 2019
1 parent 4f8fc89 commit 6b83788
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/builtin/tags/include.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions src/builtin/tags/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 28 additions & 18 deletions src/liquid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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<ITemplate[]> {
public async parseFile (file: string, opts?: LiquidOptions): Promise<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 = 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<ITemplate[]> {
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<any> {
Expand Down Expand Up @@ -127,21 +139,19 @@ export class Liquid {
return err
}

private setCache<T extends GetTemplateResult> (filepath: string, tpl: T): T {
if (tpl === undefined) return tpl
private setCache<T extends nullableTemplates> (filepath: string, tpl: T): T {
this.cache[filepath] = tpl
return tpl
}

private respectCache (filepath: string, resolver: () => GetTemplateResult): GetTemplateResult
private respectCache (filepath: string, resolver: () => Promise<GetTemplateResult>): Promise<GetTemplateResult>
private respectCache (filepath: string, resolver: () => GetTemplateResult | Promise<GetTemplateResult>): GetTemplateResult | Promise<GetTemplateResult> {
private respectCache (filepath: string, resolver: () => nullableTemplates): nullableTemplates
private respectCache (filepath: string, resolver: () => Promise<nullableTemplates>): Promise<nullableTemplates>
private respectCache (filepath: string, resolver: () => nullableTemplates | Promise<nullableTemplates>): nullableTemplates | Promise<nullableTemplates> {
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)
}
}

0 comments on commit 6b83788

Please sign in to comment.