mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 04:40:39 -07:00
feat: renderSync, parseAndRenderSync and renderFileSync, see #48
This commit is contained in:
+17
-4
@@ -4,13 +4,26 @@ import { ITemplate } from '../template/itemplate'
|
||||
import { Emitter } from './emitter'
|
||||
|
||||
export class Render {
|
||||
public async renderTemplates (templates: ITemplate[], ctx: Context, emitter = new Emitter()) {
|
||||
public async renderTemplates (templates: ITemplate[], ctx: Context, emitter = new Emitter()): Promise<string> {
|
||||
for (const tpl of templates) {
|
||||
try {
|
||||
await tpl.render(ctx, emitter)
|
||||
const html = await tpl.render(ctx, emitter)
|
||||
html && emitter.write(html)
|
||||
if (emitter.break || emitter.continue) break
|
||||
} catch (e) {
|
||||
if (e.name === 'RenderBreakError') throw e
|
||||
throw e.name === 'RenderError' ? e : new RenderError(e, tpl)
|
||||
throw RenderError.is(e) ? e : new RenderError(e, tpl)
|
||||
}
|
||||
}
|
||||
return emitter.html
|
||||
}
|
||||
public renderTemplatesSync (templates: ITemplate[], ctx: Context, emitter = new Emitter()): string {
|
||||
for (const tpl of templates) {
|
||||
try {
|
||||
const html = tpl.renderSync(ctx, emitter)
|
||||
html && !(html instanceof Promise) && emitter.write(html)
|
||||
if (emitter.break || emitter.continue) break
|
||||
} catch (e) {
|
||||
throw RenderError.is(e) ? e : new RenderError(e, tpl)
|
||||
}
|
||||
}
|
||||
return emitter.html
|
||||
|
||||
Reference in New Issue
Block a user