mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-14 20:00:39 -07:00
feat: alias getTemplate() to parseFile()
* deprecate Liquid#getTemplate: use Liquid#parseFile instead * deprecate Liquid#getTemplateSync: use Liquid#parseFileSync instead
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
+28
-18
@@ -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<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> {
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user