Templates rendering plugin support for Fastify.
point-of-view decorates the reply interface with the
view method for manage view engines that can be used to render templates responses.
Currently supports the following templates engines:
point-of-view will heavily cache the templates file and functions, while in
development will reload every time the template file and function.
Note that at least Fastify
v0.13.1 is needed.
The benchmark were run with the files in the
benchmark folder with the
The data has been taken with:
autocannon -c 100 -d 5 -p 10 localhost:3000
- Express: 8.8k req/sec
- Fastify: 15.6k req/sec
npm install point-of-view --save
const fastify =fastifyfastifyfastify
Or render a template directly with the
// With a promiseconst html = await fastify// or with a callbackfastify
If you want to set a fixed templates folder, or pass some options to the template engines:
If you want to omit view extension, you can add
includeViewExtension property as following:
Note that to use include files with ejs you also need:
// get a reference to resolveconst resolve = resolve// other code ...// in template engine options configure how to resolve templates folderoptions:filename:
and in ejs template files (for example templates/index.ejs) use something like:
<% include templates/header.ejs %>
To use partials in mustache you will need to pass the names and paths in the options parameter:
options:partials:header: 'header.mustache'footer: 'footer.mustache'
By default views are served with the mime type 'text/html; charset=utf-8', but you can specify a different value using the type function of reply, or by specifying the desired charset in the property 'charset' in the opts object given to the plugin.
This project is kindly sponsored by:
Licensed under MIT.