@laminarjs/handlebars
TypeScript icon, indicating that this package has built-in type declarations

0.15.0 • Public • Published

Laminar Handlebars

Handlebars implementation for the laminar http server.

Usage

examples/html.ts

import { init, router, get, post, HttpService, htmlOk } from '@laminarjs/laminar';
import { handlebarsMiddleware } from '@laminarjs/handlebars';
import { join } from 'path';

const handlebars = handlebarsMiddleware({ dir: join(__dirname, 'templates-html') });

const server = new HttpService({
  listener: handlebars(
    router(
      get('/', async ({ hbs }) => htmlOk(hbs('index'))),
      post('/result', async ({ hbs, body: { name } }) => htmlOk(hbs('result', { name }))),
    ),
  ),
});

init({ initOrder: [server], logger: console });

When you set dir, it will load and compile templates in views and partials folders.

Creating the middleware would crawl through the directory and all of its subdirectories, extracting handlebar files and compiling them.

Custom response options

examples/yaml.ts

import { init, router, get, HttpService, yamlOk, yamlBadRequest } from '@laminarjs/laminar';
import { handlebarsMiddleware } from '@laminarjs/handlebars';
import { join } from 'path';

const handlebars = handlebarsMiddleware({ dir: join(__dirname, 'templates-yaml'), views: 'yaml', extension: 'hbr' });

const http = new HttpService({
  listener: handlebars(
    router(
      get('/', async ({ hbs }) => yamlBadRequest(hbs('index.yaml'), { 'X-Index': 'true' })),
      get('/swagger.yaml', async ({ hbs }) => yamlOk(hbs('swagger.yaml', { version: 10 }))),
    ),
  ),
});

init({ initOrder: [http], logger: console });

Usage without middleware

You can also create the handblebars renderer directly without going through a middleware

examples/direct.ts

import { init, router, get, post, HttpService, htmlOk } from '@laminarjs/laminar';
import { handlebars } from '@laminarjs/handlebars';
import { join } from 'path';

const hbs = handlebars({ dir: join(__dirname, 'templates-html') });

const http = new HttpService({
  listener: router(
    get('/', async () => htmlOk(hbs('index'))),
    post('/result', async ({ body: { name } }) => htmlOk(hbs('result', { name }))),
  ),
});

init({ initOrder: [http], logger: console });

Caching options

By default handlebars middleware would preload all the templates and keep them in memory, but you can control the caching behaviour with the cacheType option. Possible values:

  • preload - load all the templates ones into memory
  • expiry - load partials when needed and keep them in cache, but check the file's mtime and reload template if changed
  • none - do not cache templates and load them on every request.

examples/expiry-cache.ts

import { init, router, get, post, HttpService, htmlOk } from '@laminarjs/laminar';
import { handlebarsMiddleware } from '@laminarjs/handlebars';
import { join } from 'path';

const handlebars = handlebarsMiddleware({ dir: join(__dirname, 'templates-html'), cacheType: 'expiry' });

const http = new HttpService({
  listener: handlebars(
    router(
      get('/', async ({ hbs }) => htmlOk(hbs('index'))),
      post('/result', async ({ hbs, body: { name } }) => htmlOk(hbs('result', { name }))),
    ),
  ),
});

init({ initOrder: [http], logger: console });

Running the tests

You can run the tests with:

yarn test

Coding style (linting, etc) tests

Style is maintained with prettier and eslint

yarn lint

Deployment

Deployment is preferment by yarn automatically on merge / push to main, but you'll need to bump the package version numbers yourself. Only updated packages with newer versions will be pushed to the npm registry.

Contributing

Have a bug? File an issue with a simple example that reproduces this so we can take a look & confirm.

Want to make a change? Submit a PR, explain why it's useful, and make sure you've updated the docs (this file) and the tests (see test folder).

License

This project is licensed under Apache 2 - see the LICENSE file for details

Readme

Keywords

none

Package Sidebar

Install

npm i @laminarjs/handlebars

Weekly Downloads

0

Version

0.15.0

License

Apache-2.0

Unpacked Size

12.7 kB

Total Files

5

Last publish

Collaborators

  • ikerin