Plugin for rendering Squirrelly Templates
node >= 6
fastify >= 2.0
npm install fastify-squirrelly
const fastify = require('fastify')();
fastify.register(require('fastify-squirrelly'));
fastify.get('/', (req, reply) => {
reply.sqrly('template-name', {data: ...});
});
fastify.listen(3000, err => {
if(err) throw err;
});decorator- change the decorator name. defaultsqrlyautoEscape- set autoEscaping on squirrelly. defaultfalsecharset- default utf-8templates- directory templates are read from. default (__dirname, "/templates")partials- directory partials are read from. default (__dirname, "/partials")helpers- directory helpers are read from. default (__dirname, "/helpers")filters- directory filters are read from. default (__dirname, "/filters")nativeHelpers- directory nativeHelpers are read from. default (__dirname, "/nativeHelpers")debug- Allows you to see the template data as json. defaultfalse
This example covers creating a squirrelly template that renders Hello World! and serving it from a fastify server. The completed example can be found in examples/hello-world
npm install fastify fastify-squirrelly
// server.js
const fastify = require('fastify')({logger: true});
fastify.register(require('fastify-squirrelly'));
fastify.get('/', (req, reply) => {
reply.sqrly('hello', {name: 'World'});
});
fastify.listen(3000, err => {
if(err) throw err;
});create a folder called templates in the same directory as the server file. Then create a file named hello.html in that folder.
.
+-- server.js
+-- templates
| +-- hello.html
<!-- hello.html -->
<h1>Hello, {{name}}!</h1>node server.js
Open a browser and go to localhost:3000 and you should see Hello, World!.
Congrats! you just rendered your first template using fastify-squirrelly.
Try passing a value other than 'World' and restart the server.