feat: renderSync, parseAndRenderSync and renderFileSync, see #48

This commit is contained in:
harttle
2019-08-26 10:15:43 -05:00
committed by Jun Yang
parent 8028f82499
commit 7fb01ad69a
66 changed files with 896 additions and 272 deletions
+17 -4
View File
@@ -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