Add view engine unit - handlebars - and response helpers
This commit is contained in:
60
lib/src/unit/ViewEngine.ts
Normal file
60
lib/src/unit/ViewEngine.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import LifecycleUnit from '../lifecycle/Unit.ts'
|
||||
import {Unit} from '../lifecycle/decorators.ts'
|
||||
import {Handlebars} from '../external/http.ts'
|
||||
import {Logging} from '../service/logging/Logging.ts'
|
||||
import {fs} from '../external/std.ts'
|
||||
|
||||
@Unit()
|
||||
export default class ViewEngine extends LifecycleUnit {
|
||||
protected _handlebars!: Handlebars
|
||||
|
||||
// TODO include basic app info in view data
|
||||
|
||||
constructor(
|
||||
protected readonly logger: Logging,
|
||||
) {
|
||||
super()
|
||||
}
|
||||
|
||||
async up() {
|
||||
this.logger.info(`Setting views base dir: ${this.app.app_path('http', 'views')}`)
|
||||
this._handlebars = new Handlebars({
|
||||
baseDir: this.app.app_path('http', 'views'),
|
||||
extname: '.hbs',
|
||||
layoutsDir: 'layouts',
|
||||
partialsDir: 'partials',
|
||||
defaultLayout: 'main',
|
||||
helpers: undefined,
|
||||
compilerOptions: undefined,
|
||||
})
|
||||
|
||||
const main_layout_path = this.app.app_path('http', 'views', 'layouts', 'main.hbs')
|
||||
if ( !(await fs.exists(main_layout_path)) ) {
|
||||
this.logger.warn(`Unable to open main view layout file: ${main_layout_path}`)
|
||||
this.logger.warn(`Unless you are using a custom layout, this could cause errors.`)
|
||||
}
|
||||
|
||||
const partials_path = this.app.app_path('http', 'views', 'partials')
|
||||
if ( !(await fs.exists(partials_path)) ) {
|
||||
this.logger.warn(`Unable to open view partials directory: ${partials_path}`)
|
||||
this.logger.warn(`This directory must exist for the view engine to function, even if it is empty.`)
|
||||
}
|
||||
}
|
||||
|
||||
get handlebars(): Handlebars {
|
||||
return this._handlebars
|
||||
}
|
||||
|
||||
async render(view: string, args?: any, layout?: string): Promise<string> {
|
||||
this.logger.debug(`Rendering view: ${view}`)
|
||||
return this.handlebars.renderView(view, args, layout)
|
||||
}
|
||||
|
||||
async partial(view: string, args?: any) {
|
||||
const parts = `${view}.hbs`.split(':')
|
||||
const resolved = this.app.app_path('http', 'views', ...parts)
|
||||
|
||||
this.logger.debug(`Rendering partial: ${view} from ${resolved}`)
|
||||
return this.handlebars.render(resolved, args)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user