2021-06-03 03:36:25 +00:00
|
|
|
import {AppClass} from '../lifecycle/AppClass'
|
|
|
|
import {Config} from '../service/Config'
|
|
|
|
import {Container} from '../di'
|
2021-11-27 16:30:49 +00:00
|
|
|
import {ErrorWithContext, hasOwnProperty, Maybe, UniversalPath} from '../util'
|
2021-06-29 06:44:07 +00:00
|
|
|
import {Routing} from '../service/Routing'
|
2021-11-27 16:30:49 +00:00
|
|
|
import {RequestLocalStorage} from '../http/RequestLocalStorage'
|
|
|
|
import {Request} from '../http/lifecycle/Request'
|
2021-03-09 00:07:55 +00:00
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/**
|
|
|
|
* Abstract base class for rendering views via different view engines.
|
|
|
|
*/
|
2021-03-09 00:07:55 +00:00
|
|
|
export abstract class ViewEngine extends AppClass {
|
|
|
|
protected readonly config: Config
|
2021-06-03 03:36:25 +00:00
|
|
|
|
2021-06-29 06:44:07 +00:00
|
|
|
protected readonly routing: Routing
|
|
|
|
|
2021-11-27 16:30:49 +00:00
|
|
|
protected readonly request: RequestLocalStorage
|
|
|
|
|
2021-03-09 00:07:55 +00:00
|
|
|
protected readonly debug: boolean
|
|
|
|
|
2021-06-24 05:14:04 +00:00
|
|
|
protected readonly namespaces: {[key: string]: UniversalPath} = {}
|
|
|
|
|
2021-11-27 16:30:49 +00:00
|
|
|
protected readonly globals: {[key: string]: (req: Maybe<Request>) => any} = {}
|
|
|
|
|
2021-03-09 00:07:55 +00:00
|
|
|
constructor() {
|
|
|
|
super()
|
|
|
|
this.config = Container.getContainer().make(Config)
|
2021-06-29 06:44:07 +00:00
|
|
|
this.routing = Container.getContainer().make(Routing)
|
2021-11-27 16:30:49 +00:00
|
|
|
this.request = Container.getContainer().make(RequestLocalStorage)
|
2021-03-09 00:07:55 +00:00
|
|
|
this.debug = (this.config.get('server.mode', 'production') === 'development'
|
|
|
|
|| this.config.get('server.debug', false))
|
|
|
|
}
|
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/**
|
|
|
|
* Get the UniversalPath to the base directory where views are loaded from.
|
|
|
|
*/
|
2021-03-09 00:07:55 +00:00
|
|
|
public get path(): UniversalPath {
|
|
|
|
return this.app().appPath(...['resources', 'views']) // FIXME allow configuring
|
|
|
|
}
|
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/**
|
|
|
|
* Given a template string and a set of variables for the view, render the string to HTML and return it.
|
|
|
|
* @param templateString
|
|
|
|
* @param locals
|
|
|
|
*/
|
2021-03-09 00:07:55 +00:00
|
|
|
public abstract renderString(templateString: string, locals: {[key: string]: any}): string | Promise<string>
|
2021-03-25 13:50:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Given the canonical name of a template file, render the file using the provided variables.
|
|
|
|
* @param templateName
|
|
|
|
* @param locals
|
|
|
|
*/
|
2021-03-09 00:07:55 +00:00
|
|
|
public abstract renderByName(templateName: string, locals: {[key: string]: any}): string | Promise<string>
|
2021-06-24 05:14:04 +00:00
|
|
|
|
2021-06-29 06:44:07 +00:00
|
|
|
/**
|
|
|
|
* Get the file extension of template files of this engine.
|
|
|
|
* @example `.pug`
|
|
|
|
*/
|
|
|
|
public abstract getFileExtension(): string
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the global variables that should be passed to every view rendered.
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected getGlobals(): {[key: string]: any} {
|
2021-11-27 16:30:49 +00:00
|
|
|
const globals: {[key: string]: any} = {
|
2021-06-29 06:44:07 +00:00
|
|
|
app: this.app(),
|
|
|
|
config: (key: string, fallback?: any) => this.config.get(key, fallback),
|
|
|
|
asset: (...parts: string[]) => this.routing.getAssetPath(...parts).toRemote,
|
|
|
|
vendor: (namespace: string, ...parts: string[]) => this.routing.getVendorPath(namespace, ...parts).toRemote,
|
2021-07-17 17:49:07 +00:00
|
|
|
named: (name: string) => this.routing.getNamedPath(name).toRemote,
|
|
|
|
route: (...parts: string[]) => this.routing.getAppUrl().concat(...parts).toRemote,
|
|
|
|
hasRoute: (name: string) => this.routing.hasNamedRoute(name),
|
2021-06-29 06:44:07 +00:00
|
|
|
}
|
2021-11-27 16:30:49 +00:00
|
|
|
|
|
|
|
const req = this.request.get()
|
|
|
|
if ( req ) {
|
|
|
|
globals.request = () => req
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( const key in this.globals ) {
|
|
|
|
if ( !hasOwnProperty(this.globals, key) ) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
globals[key] = this.globals[key](req)
|
|
|
|
}
|
|
|
|
|
|
|
|
return globals
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a new factory that produces a global available in the templates by default.
|
|
|
|
* @param name
|
|
|
|
* @param factory
|
|
|
|
*/
|
|
|
|
public registerGlobalFactory(name: string, factory: (req: Maybe<Request>) => any): this {
|
|
|
|
this.globals[name] = factory
|
|
|
|
return this
|
2021-06-29 06:44:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a path as a root for rendering views prefixed with the given namespace.
|
|
|
|
* @param namespace
|
|
|
|
* @param basePath
|
|
|
|
*/
|
2021-06-24 05:14:04 +00:00
|
|
|
public registerNamespace(namespace: string, basePath: UniversalPath): this {
|
|
|
|
if ( namespace.startsWith('@') ) {
|
|
|
|
namespace = namespace.substr(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.namespaces[namespace] = basePath
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
2021-06-29 06:44:07 +00:00
|
|
|
/**
|
|
|
|
* Given the name of a template, get a UniversalPath pointing to its file.
|
|
|
|
* @param templateName
|
|
|
|
*/
|
2021-06-24 05:14:04 +00:00
|
|
|
public resolveName(templateName: string): UniversalPath {
|
|
|
|
let path = this.path
|
|
|
|
if ( templateName.startsWith('@') ) {
|
|
|
|
const [namespace, ...parts] = templateName.split(':')
|
|
|
|
path = this.namespaces[namespace.substr(1)]
|
|
|
|
|
|
|
|
if ( !path ) {
|
|
|
|
throw new ErrorWithContext('Invalid template namespace: ' + namespace, {
|
|
|
|
namespace,
|
|
|
|
templateName,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
templateName = parts.join(':')
|
|
|
|
}
|
|
|
|
|
2021-06-29 06:44:07 +00:00
|
|
|
if ( !templateName.endsWith(this.getFileExtension()) ) {
|
|
|
|
templateName += this.getFileExtension()
|
2021-06-24 05:14:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return path.concat(...templateName.split(':'))
|
|
|
|
}
|
|
|
|
|
2021-06-29 06:44:07 +00:00
|
|
|
/**
|
|
|
|
* Given the name of a template, get a UniversalPath to the root of the tree where
|
|
|
|
* that template resides.
|
|
|
|
* @param templateName
|
|
|
|
*/
|
2021-06-24 05:14:04 +00:00
|
|
|
public resolveBasePath(templateName: string): UniversalPath {
|
|
|
|
let path = this.path
|
|
|
|
if ( templateName.startsWith('@') ) {
|
|
|
|
const [namespace] = templateName.split(':')
|
|
|
|
path = this.namespaces[namespace.substr(1)]
|
|
|
|
|
|
|
|
if ( !path ) {
|
|
|
|
throw new ErrorWithContext('Invalid template namespace: ' + namespace, {
|
|
|
|
namespace,
|
|
|
|
templateName,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return path
|
|
|
|
}
|
2021-03-09 00:07:55 +00:00
|
|
|
}
|