|
renderSync: function (ctx: Context, hash: Hash, emitter: Emitter) { |
|
let filepath |
|
if (ctx.opts.dynamicPartials) { |
|
if (quotedLine.exec(this.value)) { |
|
const template = this.value.slice(1, -1) |
|
filepath = this.liquid.parseAndRenderSync(template, ctx.getAll(), ctx.opts) |
|
} else { |
|
filepath = new Expression(this.value).valueSync(ctx) |
|
} |
|
} else { |
|
filepath = this.staticValue |
|
} |
|
assert(filepath, `cannot include with empty filename`) |
|
|
|
const originBlocks = ctx.getRegister('blocks') |
|
const originBlockMode = ctx.getRegister('blockMode') |
|
|
|
ctx.setRegister('blocks', {}) |
|
ctx.setRegister('blockMode', BlockMode.OUTPUT) |
|
if (this.with) { |
|
hash[filepath] = new Expression(this.with).evaluateSync(ctx) |
|
} |
|
const templates = this.liquid.parseFileSync(filepath, ctx.opts) |
|
ctx.push(hash) |
|
this.liquid.renderer.renderTemplatesSync(templates, ctx, emitter) |
|
ctx.pop() |
|
ctx.setRegister('blocks', originBlocks) |
|
ctx.setRegister('blockMode', originBlockMode) |
|
}, |
|
|
|
render: async function (ctx: Context, hash: Hash, emitter: Emitter) { |
|
let filepath |
|
if (ctx.opts.dynamicPartials) { |
|
if (quotedLine.exec(this.value)) { |
|
const template = this.value.slice(1, -1) |
|
filepath = await this.liquid.parseAndRender(template, ctx.getAll(), ctx.opts) |
|
} else { |
|
filepath = await new Expression(this.value).value(ctx) |
|
} |
|
} else { |
|
filepath = this.staticValue |
|
} |
|
assert(filepath, `cannot include with empty filename`) |
|
|
|
const originBlocks = ctx.getRegister('blocks') |
|
const originBlockMode = ctx.getRegister('blockMode') |
|
|
|
ctx.setRegister('blocks', {}) |
|
ctx.setRegister('blockMode', BlockMode.OUTPUT) |
|
if (this.with) { |
|
hash[filepath] = await new Expression(this.with).evaluate(ctx) |
|
} |
|
const templates = await this.liquid.parseFile(filepath, ctx.opts) |
|
ctx.push(hash) |
|
await this.liquid.renderer.renderTemplates(templates, ctx, emitter) |
|
ctx.pop() |
|
ctx.setRegister('blocks', originBlocks) |
|
ctx.setRegister('blockMode', originBlockMode) |
|
} |
This will reduce the overall code size, reduce redundency and remove dependency to Promsie (which is trivial).
For example in the builtin/tags/include.ts:
liquidjs/src/builtin/tags/include.ts
Lines 20 to 78 in ab8db53
The
render()method andrenderSync()method are almost the same. The tricky part is Async and Sync functions cannot be easily wrapped so that they expose the same interface. Maybe refactor into callbacks or generators can address this issue.