Support registering namespaced view directories; add lib() universal path
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2021-06-24 00:14:04 -05:00
parent a69c81ed35
commit 7506d6567d
9 changed files with 201 additions and 17 deletions

View File

@@ -20,11 +20,8 @@ export class PugViewEngine extends ViewEngine {
return compiled(locals)
}
if ( !templateName.endsWith('.pug') ) {
templateName += '.pug'
}
const filePath = this.path.concat(...templateName.split(':'))
compiled = pug.compileFile(filePath.toLocal, this.getOptions())
const filePath = this.resolveName(templateName)
compiled = pug.compileFile(filePath.toLocal, this.getOptions(templateName))
this.compileCache[templateName] = compiled
return compiled(locals)
@@ -34,9 +31,9 @@ export class PugViewEngine extends ViewEngine {
* Get the object of options passed to Pug's compile methods.
* @protected
*/
protected getOptions(): pug.Options {
protected getOptions(templateName?: string): pug.Options {
return {
basedir: this.path.toLocal,
basedir: templateName ? this.resolveBasePath(templateName).toLocal : this.path.toLocal,
debug: this.debug,
compileDebug: this.debug,
globals: [],

View File

@@ -1,7 +1,7 @@
import {AppClass} from '../lifecycle/AppClass'
import {Config} from '../service/Config'
import {Container} from '../di'
import {UniversalPath} from '../util'
import {ErrorWithContext, UniversalPath} from '../util'
/**
* Abstract base class for rendering views via different view engines.
@@ -11,6 +11,8 @@ export abstract class ViewEngine extends AppClass {
protected readonly debug: boolean
protected readonly namespaces: {[key: string]: UniversalPath} = {}
constructor() {
super()
this.config = Container.getContainer().make(Config)
@@ -38,4 +40,53 @@ export abstract class ViewEngine extends AppClass {
* @param locals
*/
public abstract renderByName(templateName: string, locals: {[key: string]: any}): string | Promise<string>
public registerNamespace(namespace: string, basePath: UniversalPath): this {
if ( namespace.startsWith('@') ) {
namespace = namespace.substr(1)
}
this.namespaces[namespace] = basePath
return this
}
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(':')
}
if ( !templateName.endsWith('.pug') ) {
templateName += '.pug'
}
return path.concat(...templateName.split(':'))
}
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
}
}