mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 04:10:40 -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) {
|
if (this.with) {
|
||||||
hash[filepath] = new Expression(this.with).evaluateSync(ctx)
|
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)
|
ctx.push(hash)
|
||||||
this.liquid.renderer.renderTemplatesSync(templates, ctx, emitter)
|
this.liquid.renderer.renderTemplatesSync(templates, ctx, emitter)
|
||||||
ctx.pop()
|
ctx.pop()
|
||||||
@@ -69,7 +69,7 @@ export default {
|
|||||||
if (this.with) {
|
if (this.with) {
|
||||||
hash[filepath] = await new Expression(this.with).evaluate(ctx)
|
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)
|
ctx.push(hash)
|
||||||
await this.liquid.renderer.renderTemplates(templates, ctx, emitter)
|
await this.liquid.renderer.renderTemplates(templates, ctx, emitter)
|
||||||
ctx.pop()
|
ctx.pop()
|
||||||
|
|||||||
@@ -39,8 +39,8 @@ export default {
|
|||||||
blocks[''] = html
|
blocks[''] = html
|
||||||
}
|
}
|
||||||
const templates = ctx.sync
|
const templates = ctx.sync
|
||||||
? this.liquid.getTemplateSync(layout, ctx.opts)
|
? this.liquid.parseFileSync(layout, ctx.opts)
|
||||||
: await this.liquid.getTemplate(layout, ctx.opts)
|
: await this.liquid.parseFile(layout, ctx.opts)
|
||||||
ctx.push(hash)
|
ctx.push(hash)
|
||||||
ctx.setRegister('blockMode', BlockMode.OUTPUT)
|
ctx.setRegister('blockMode', BlockMode.OUTPUT)
|
||||||
const partial = ctx.sync
|
const partial = ctx.sync
|
||||||
|
|||||||
+28
-18
@@ -15,7 +15,7 @@ import { LiquidOptions, normalizeStringArray, NormalizedFullOptions, applyDefaul
|
|||||||
import { FilterImplOptions } from './template/filter/filter-impl-options'
|
import { FilterImplOptions } from './template/filter/filter-impl-options'
|
||||||
import IFS from './fs/ifs'
|
import IFS from './fs/ifs'
|
||||||
|
|
||||||
type GetTemplateResult = ITemplate[] | undefined
|
type nullableTemplates = ITemplate[] | null
|
||||||
|
|
||||||
export * from './types'
|
export * from './types'
|
||||||
|
|
||||||
@@ -59,40 +59,52 @@ export class Liquid {
|
|||||||
const tpl = this.parse(html)
|
const tpl = this.parse(html)
|
||||||
return this.renderSync(tpl, scope, opts)
|
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 options = { ...this.options, ...normalize(opts) }
|
||||||
const paths = options.root.map(root => this.fs.resolve(root, file, options.extname))
|
const paths = options.root.map(root => this.fs.resolve(root, file, options.extname))
|
||||||
|
|
||||||
for (const filepath of paths) {
|
for (const filepath of paths) {
|
||||||
const tpl = this.respectCache(filepath, () => {
|
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)
|
return this.parse(this.fs.readFileSync(filepath), filepath)
|
||||||
})
|
})
|
||||||
if (tpl) return tpl
|
if (tpl !== null) return tpl
|
||||||
}
|
}
|
||||||
|
|
||||||
throw this.lookupError(file, options.root)
|
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 options = { ...this.options, ...normalize(opts) }
|
||||||
const paths = options.root.map(root => this.fs.resolve(root, file, options.extname))
|
const paths = options.root.map(root => this.fs.resolve(root, file, options.extname))
|
||||||
|
|
||||||
for (const filepath of paths) {
|
for (const filepath of paths) {
|
||||||
const tpl = await this.respectCache(filepath, async () => {
|
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)
|
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)
|
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) {
|
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)
|
return this.render(templates, ctx, opts)
|
||||||
}
|
}
|
||||||
public renderFileSync (file: string, ctx?: object, opts?: LiquidOptions) {
|
public renderFileSync (file: string, ctx?: object, opts?: LiquidOptions) {
|
||||||
const options = normalize(opts)
|
const options = normalize(opts)
|
||||||
const templates = this.getTemplateSync(file, options)
|
const templates = this.parseFileSync(file, options)
|
||||||
return this.renderSync(templates, ctx, opts)
|
return this.renderSync(templates, ctx, opts)
|
||||||
}
|
}
|
||||||
public async evalValue (str: string, ctx: Context): Promise<any> {
|
public async evalValue (str: string, ctx: Context): Promise<any> {
|
||||||
@@ -127,21 +139,19 @@ export class Liquid {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
private setCache<T extends GetTemplateResult> (filepath: string, tpl: T): T {
|
private setCache<T extends nullableTemplates> (filepath: string, tpl: T): T {
|
||||||
if (tpl === undefined) return tpl
|
|
||||||
this.cache[filepath] = tpl
|
this.cache[filepath] = tpl
|
||||||
return tpl
|
return tpl
|
||||||
}
|
}
|
||||||
|
|
||||||
private respectCache (filepath: string, resolver: () => GetTemplateResult): GetTemplateResult
|
private respectCache (filepath: string, resolver: () => nullableTemplates): nullableTemplates
|
||||||
private respectCache (filepath: string, resolver: () => Promise<GetTemplateResult>): Promise<GetTemplateResult>
|
private respectCache (filepath: string, resolver: () => Promise<nullableTemplates>): Promise<nullableTemplates>
|
||||||
private respectCache (filepath: string, resolver: () => GetTemplateResult | Promise<GetTemplateResult>): GetTemplateResult | Promise<GetTemplateResult> {
|
private respectCache (filepath: string, resolver: () => nullableTemplates | Promise<nullableTemplates>): nullableTemplates | Promise<nullableTemplates> {
|
||||||
if (!this.options.cache) return resolver()
|
if (!this.options.cache) return resolver()
|
||||||
if (this.cache[filepath]) return this.cache[filepath]
|
if (this.cache[filepath]) return this.cache[filepath]
|
||||||
|
|
||||||
const tpl = resolver()
|
const tpl = resolver()
|
||||||
if (tpl instanceof Promise) {
|
const setCacheIfDefined = (tpl: nullableTemplates) => tpl === null ? null : this.setCache(filepath, tpl)
|
||||||
return tpl.then(c => this.setCache(filepath, c))
|
return tpl instanceof Promise ? tpl.then(setCacheIfDefined) : setCacheIfDefined(tpl)
|
||||||
}
|
|
||||||
return this.setCache(filepath, tpl)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user