parent
3e4f8f00f2
commit
b5bde7d077
@ -0,0 +1,11 @@
|
||||
import Controller from '../../../lib/src/http/Controller.ts'
|
||||
import {Request} from '../../../lib/src/http/Request.ts'
|
||||
import {view} from '../../../lib/src/http/response/helpers.ts'
|
||||
|
||||
export default class HomeController extends Controller {
|
||||
|
||||
get_home(request: Request) {
|
||||
return view('home', { request })
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
import { RouterDefinition } from '../../../lib/src/http/type/RouterDefinition.ts'
|
||||
|
||||
export default {
|
||||
prefix: '/',
|
||||
middleware: [],
|
||||
get: {
|
||||
'/': 'controller::Home.get_home',
|
||||
},
|
||||
} as RouterDefinition
|
@ -0,0 +1 @@
|
||||
<h1>Welcome to Daton!</h1>
|
@ -0,0 +1,14 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
{{#if title}}
|
||||
<title>{{ title }} | Daton</title>
|
||||
{{else}}
|
||||
<title>Daton</title>
|
||||
{{/if}}
|
||||
</head>
|
||||
<body>
|
||||
{{{ body }}}
|
||||
</body>
|
||||
</html>
|
@ -1,2 +1,3 @@
|
||||
export * from 'https://deno.land/std@0.53.0/http/server.ts'
|
||||
export * from 'https://deno.land/std@0.53.0/http/cookie.ts'
|
||||
export { Handlebars } from 'https://deno.land/x/handlebars/mod.ts'
|
@ -0,0 +1,18 @@
|
||||
import ResponseFactory from './ResponseFactory.ts'
|
||||
import ViewEngine from '../../unit/ViewEngine.ts'
|
||||
import {Request} from '../Request.ts'
|
||||
|
||||
export default class PartialViewResponseFactory extends ResponseFactory {
|
||||
constructor(
|
||||
public readonly view: string,
|
||||
public readonly context?: any,
|
||||
) {
|
||||
super()
|
||||
}
|
||||
|
||||
public async write(request: Request) {
|
||||
const views: ViewEngine = this.make(ViewEngine)
|
||||
request.response.body = await views.partial(this.view, this.context)
|
||||
return request
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
import ResponseFactory from './ResponseFactory.ts'
|
||||
import ViewEngine from '../../unit/ViewEngine.ts'
|
||||
import {Request} from '../Request.ts'
|
||||
|
||||
export default class ViewResponseFactory extends ResponseFactory {
|
||||
constructor(
|
||||
public readonly view: string,
|
||||
public readonly context?: any,
|
||||
public readonly layout?: string,
|
||||
) {
|
||||
super()
|
||||
}
|
||||
|
||||
public async write(request: Request) {
|
||||
const views: ViewEngine = this.make(ViewEngine)
|
||||
request.response.body = await views.render(this.view, this.context, this.layout)
|
||||
return request
|
||||
}
|
||||
}
|
@ -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)
|
||||
}
|
||||
}
|
Loading…
Reference in new issue