Add support for view engines, PNPM
This commit is contained in:
33
src/views/PugViewEngine.ts
Normal file
33
src/views/PugViewEngine.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import {ViewEngine} from "./ViewEngine"
|
||||
import {Injectable} from "@extollo/di"
|
||||
import * as pug from "pug"
|
||||
|
||||
@Injectable()
|
||||
export class PugViewEngine extends ViewEngine {
|
||||
protected compileCache: {[key: string]: ((locals?: pug.LocalsObject) => string)} = {}
|
||||
|
||||
public renderString(templateString: string, locals: { [p: string]: any }): string | Promise<string> {
|
||||
return pug.compile(templateString, this.getOptions())(locals)
|
||||
}
|
||||
|
||||
public renderByName(templateName: string, locals: { [p: string]: any }): string | Promise<string> {
|
||||
let compiled = this.compileCache[templateName]
|
||||
if ( compiled ) return compiled(locals)
|
||||
|
||||
if ( !templateName.endsWith('.pug') ) templateName += '.pug'
|
||||
const filePath = this.path.concat(...templateName.split(':'))
|
||||
compiled = pug.compileFile(filePath.toLocal, this.getOptions())
|
||||
|
||||
this.compileCache[templateName] = compiled
|
||||
return compiled(locals)
|
||||
}
|
||||
|
||||
protected getOptions() {
|
||||
return {
|
||||
basedir: this.path.toLocal,
|
||||
debug: this.debug,
|
||||
compileDebug: this.debug,
|
||||
globals: [],
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user