Skip to content
Cédric Belin edited this page Jun 16, 2024 · 4 revisions

A Koa view renderer based on the Eta template engine.

Quick start

Install the latest version of Eta for Koa with npm package manager:

npm install @cedx/koa-eta

For detailed instructions, see the installation guide.

Usage

This library provides an eta() function that you simply invoke by passing the instance of your Koa application as an argument.

import eta from "@cedx/koa-eta";
import Koa from "koa";

// Initialize the application.
const app = new Koa;
eta(app);

This function will add two new methods render() and renderPdf() to the request context, that you can use to render Eta view templates.

// Render the "view.eta" template.
app.use(async ctx => {
  return ctx.render("view");
});

For more information, visit the pages dedicated to each of them:

Options

The eta() function accepts an option object as a second argument. These options are directly passed to the Eta constructor.

The most important one is the views option that let you specify the path of the directory containing your view templates.

import {join} from "node:path";
import eta from "@cedx/koa-eta";

// Initialize the template engine.
const directory = join(import.meta.dirname, "path/to/view/folder");
eta(app, {views: directory});

Warning

The views option must be set to use the template engine.
Otherwise, an EtaFileResolution error will be raised.

Please refer to the Eta documentation for details of all configuration options.

Clone this wiki locally