-
Notifications
You must be signed in to change notification settings - Fork 7
H. Renderers
In this section, you will learn how to create and configure an OnixJS Renderer.
Similar to configuring a data source, a renderer accepts any renderer engine you like, such as EJS, DoT, etc.
HINT: In order to expose a SOA Service to the network and therefore serverside rendering some views, you need to enable your SOA Service as stated in here.
import {ViewRenderer, IViewRenderer} from '@onixjs/core';
@ViewRenderer
class MyRenderer implements IViewRenderer {
process(view: string, args: Directory): string {
return dot.template(view, undefined, args)();
}
}
The example above is really simple, basically works as a facade for a specific renderer engine you select, in the example above we are using DoT, but you can actually use any renderer you desire.
The arguments are simply a passed string that might come from a static file or from a component string result.
Once your renderer is installed within a module context, you will be able to inject your renderer mainly within a component, but remember that injectables can also be injected within services providers.
import {Component, Inject, Router} from '@onixjs/core';
import {MyRenderer} from './my.renderer';
@Component({})
class MyComponent {
@Inject.Renderer(MyRenderer) renderer: MyRenderer;
@Router.View({
endpoint: '/myview',
file: 'some/static.file',
})
async test(req: OnixHTTPRequest, buffer: Buffer) {
return this.renderer.process(buffer.toString(), {
key: 'HELLO',
value: 'WORLD',
});
}
}
HINT: static files can be any static file, from txt to html, json, etc.