Skip to main content

Template engines

Lunatic provides options for integrating template engines. In fact, any template engine can be used: some, such as Pug can be used "out of the box", some additionally requires adapter.

Here is simple example of using template engine:

import { LunaticServer } from '@shelepuginivan/lunatic'
import { render } from 'pug'

app.renderer(render)

app.renderer

app.renderer() is a method of LunaticServer which accepts RenderFunction - a function of the following signature:

export type RenderFunction = (
source: string,
options?: Record<string, any>,
) => string

Parameters:

  • source: string - source template content string
  • options: Record<string, any> - template options, i.e. variables used in template

Note that you can write an adapter - wrapper of type RenderFunction for render method of your favorite template engine, and use it in app.renderer()

Rendering template

To render template, you can use methods Response.render() and Response.renderFile().

import { LunaticServer } from '@shelepuginivan/lunatic'
import { render } from 'pug'

app.renderer(render)
app.get('/', (_req, res) =>
res.renderFile(join(__dirname, '..', 'views', 'index.pug')),
)