Skip to content

Commit

Permalink
feat(loader): add support for registering template as a string
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jul 9, 2018
1 parent 1989461 commit 761d317
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Contracts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface ILoader {
unmount (diskName: string): void
resolve (templatePath: string, withResolver: boolean): { template: string, Presenter?: IPresenterConstructor }
makePath (templatePath: string): string
register (templatePath: string, contents: { template: string, Presenter?: IPresenterConstructor }): void
}

export interface ICompiler {
Expand Down
9 changes: 8 additions & 1 deletion src/Edge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { merge } from 'lodash'
import * as Tags from '../Tags'
import { Compiler } from '../Compiler'
import { Loader } from '../Loader'
import { ILoaderConstructor, ILoader, ITag } from '../Contracts'
import { ILoaderConstructor, ILoader, ITag, IPresenterConstructor } from '../Contracts'
import { Template } from '../Template'
import { Context } from '../Context'

Expand Down Expand Up @@ -97,6 +97,13 @@ export class Edge {
Tags[Tag.tagName] = Tag
}

/**
* Register a template as a string
*/
public static register (templatePath: string, contents: { template: string, Presenter?: IPresenterConstructor }) {
loader!.register(templatePath, contents)
}

/**
* Shorthand to `new Edge().render()` or `Edge.newUp().render()`
*/
Expand Down
23 changes: 23 additions & 0 deletions src/Loader/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { extractDiskAndTemplateName } from '../utils'

export class Loader implements ILoader {
private mountedDirs: Map<string, string> = new Map()
private preRegistered: Map<string, { template: string, Presenter?: IPresenterConstructor }> = new Map()

/**
* Returns an object of mounted directories with their public
Expand Down Expand Up @@ -70,6 +71,15 @@ export class Loader implements ILoader {
public resolve (templatePath: string, withPresenter: boolean): { template: string, Presenter?: IPresenterConstructor } {
try {
templatePath = isAbsolute(templatePath) ? templatePath : this.makePath(templatePath)

/**
* Return from pre-registered one's if exists
*/
if (this.preRegistered.get(templatePath)) {
const contents = this.preRegistered.get(templatePath)
return withPresenter ? contents! : { template: contents!.template }
}

const template = readFileSync(templatePath, 'utf-8')
const Presenter = withPresenter ? this._getPresenterForTemplate(templatePath) : undefined
return { template, Presenter }
Expand All @@ -82,6 +92,19 @@ export class Loader implements ILoader {
}
}

/**
* Register a template as a string
*/
public register (templatePath: string, contents: { template: string, Presenter?: IPresenterConstructor }) {
templatePath = isAbsolute(templatePath) ? templatePath : this.makePath(templatePath)

if (!contents.template) {
throw new Error('Make sure to define the template content for preRegistered template')
}

this.preRegistered.set(templatePath, contents)
}

/**
* Attempts to conventionally load the presenter for a given template
* using the same basePath.
Expand Down
19 changes: 19 additions & 0 deletions test/edge.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,23 @@ test.group('Template', (group) => {
assert.equal(tmpl.render('foo', {}).trim(), 'Hello nikk')
assert.equal(Edge.render('foo', {}).trim(), 'Hello guest')
})

test('register a template as a string', async (assert) => {
Edge.mount(viewsDir)
Edge.register('foo', {
template: `Hello {{ username }}`,
})

assert.equal(Edge.render('foo', { username: 'virk' }).trim(), 'Hello virk')
})

test('register a template on a named disk', async (assert) => {
Edge.mount('hello', viewsDir)

Edge.register('hello::foo', {
template: `Hello {{ username }}`,
})

assert.equal(Edge.render('hello::foo', { username: 'virk' }).trim(), 'Hello virk')
})
})

0 comments on commit 761d317

Please sign in to comment.