Skip to content

Templating

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 require you to write an adapter.

import { LunaticServer } from '@shelepuginivan/lunatic'
import { render } from 'pug'
app.renderer(render)

Set a renderer

You can set a renderer with the app.renderer method of LunaticServer.

The renderer is a function with the following signature:

(source: string, options?: Record<string, any>)

where

  • source — string content of the template
  • options — template options or context

Rendering a template

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

import { LunaticServer } from '@shelepuginivan/lunatic'
import { render } from 'pug'
app.renderer(render)
app.get('/', (_req, res) =>
res.renderFile(join(__dirname, '..', 'views', 'index.pug')),
)