-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat(renderer): support handlebar renderer engine (#18)
* feat(renderer): add interfance and @Render decorator * feat(renderer): implement hbs renderer * docs(renderer): add small tutorial * fix(renderer): add file to partials folder so it is pushed * lint(*): deno fmt * refactor(view-rendere-test): remove console.log * docs(renderer): add small info * refactor(app): change actualRendererToRenderer
- Loading branch information
1 parent
d32526f
commit 5251830
Showing
13 changed files
with
275 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Rendering HTML | ||
|
||
Building API is cool, but sometime, we want to build a simple MVC app that will | ||
render HTML. | ||
|
||
For this, Danet integrate the [Handlebars](https://handlebarsjs.com/) templating | ||
engine. | ||
|
||
## Before writing any code | ||
|
||
#### Create the following directory at your project's root | ||
|
||
``` | ||
/views | ||
/views/layouts | ||
/views/partials | ||
``` | ||
|
||
!!!info Info | ||
If you want to put these directory elsewhere, you can provide the path to `views` at runtime with `app.setViewEngineDir('my/path/to/views);` | ||
!!! | ||
|
||
#### Create a default layout called `main.hbs` with the following content: | ||
|
||
```handlebars | ||
{{{body}}} | ||
``` | ||
|
||
## Let's render things now ! | ||
|
||
First, let's create your first template called `hello.hbs` in the `views` | ||
directory. It will print 2 variables passed from your controller. | ||
|
||
```handlebars | ||
<html> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>{{ title }}</title> | ||
</head> | ||
<body> | ||
Hello {{ name }}! | ||
</body> | ||
</html> | ||
``` | ||
|
||
Now, let's tell your controller it has to render this view on a specific route: | ||
|
||
```ts | ||
@Controller('nice-controller') | ||
class MyController { | ||
@Render('hello') | ||
@Get('/') | ||
renderANiceHTML() { | ||
return { title: 'the page title', name: 'world' }; | ||
} | ||
} | ||
``` | ||
|
||
We specify the template to use with the `@Render()` decorator, and the return | ||
value of the route handler is passed to the template for rendering. | ||
|
||
Notice that the return value is an object with `title` and `name` properties, | ||
matching `title` and `name` placeholders we used in the template. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { assertEquals } from 'https://deno.land/[email protected]/testing/asserts.ts'; | ||
import * as path from 'https://deno.land/[email protected]/path/mod.ts'; | ||
import { DanetApplication } from '../src/app.ts'; | ||
import { Module } from '../src/module/decorator.ts'; | ||
import { Render } from '../src/renderer/decorator.ts'; | ||
import { Controller, Get } from '../src/router/controller/decorator.ts'; | ||
|
||
@Controller('nice-controller') | ||
class SimpleController { | ||
@Render('index') | ||
@Get('/') | ||
simpleGet() { | ||
return { title: 'my title' }; | ||
} | ||
} | ||
|
||
@Module({ | ||
controllers: [SimpleController], | ||
}) | ||
class MyModule {} | ||
|
||
Deno.test('Hbs renderer', async () => { | ||
const app = new DanetApplication(); | ||
await app.init(MyModule); | ||
const viewPath = path.dirname(path.fromFileUrl(import.meta.url)) + '/views'; | ||
app.setViewEngineDir(viewPath); | ||
const port = (await app.listen(0)).port; | ||
|
||
const res = await fetch(`http://localhost:${port}/nice-controller`, { | ||
method: 'GET', | ||
}); | ||
const text = await res.text(); | ||
assertEquals(text.includes('my title'), true); | ||
await app.close(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{title}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{{body}}} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,29 @@ | ||
import { ApplicationListenEvent } from 'https://deno.land/x/[email protected]/application.ts'; | ||
import { Application, Router } from 'https://deno.land/x/[email protected]/mod.ts'; | ||
import { FilterExecutor } from './exception/filter/executor.ts'; | ||
import { GuardExecutor } from './guard/executor.ts'; | ||
import { HookExecutor } from './hook/executor.ts'; | ||
import { hookName } from './hook/interfaces.ts'; | ||
|
||
import { Injector } from './injector/injector.ts'; | ||
import { Logger } from './logger.ts'; | ||
import { MetadataHelper } from './metadata/helper.ts'; | ||
import { moduleMetadataKey, ModuleOptions } from './module/decorator.ts'; | ||
import { HandlebarRenderer } from './renderer/handlebar.ts'; | ||
import { DanetRouter } from './router/router.ts'; | ||
import { Constructor } from './utils/constructor.ts'; | ||
|
||
export class DanetApplication { | ||
private app = new Application(); | ||
private injector = new Injector(); | ||
private hookExecutor = new HookExecutor(this.injector); | ||
public danetRouter = new DanetRouter(this.injector); | ||
private renderer = new HandlebarRenderer(); | ||
public danetRouter = new DanetRouter( | ||
this.injector, | ||
new GuardExecutor(this.injector), | ||
new FilterExecutor(), | ||
this.renderer, | ||
); | ||
private controller: AbortController = new AbortController(); | ||
private logger: Logger = new Logger('DanetApplication'); | ||
|
||
|
@@ -69,4 +78,8 @@ export class DanetApplication { | |
get router(): Router { | ||
return this.danetRouter.router; | ||
} | ||
|
||
setViewEngineDir(path: string) { | ||
this.renderer.setRootDir(path); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { MetadataHelper } from '../metadata/helper.ts'; | ||
import { ControllerConstructor } from '../router/controller/constructor.ts'; | ||
|
||
export const rendererViewFile = 'rendererViewFile'; | ||
export const Render = (fileName: string) => | ||
( | ||
// deno-lint-ignore ban-types | ||
target: ControllerConstructor | Object, | ||
propertyKey?: string | symbol, | ||
// deno-lint-ignore no-explicit-any | ||
descriptor?: TypedPropertyDescriptor<any>, | ||
) => { | ||
if (propertyKey && descriptor) { | ||
MetadataHelper.setMetadata( | ||
rendererViewFile, | ||
fileName, | ||
descriptor.value, | ||
); | ||
} | ||
}; |
Oops, something went wrong.