0.4.0 • Published 6 years ago
fastify-squirrelly v0.4.0
fastify-squirrelly
Plugin for rendering Squirrelly Templates
Requirements
node >= 6
fastify >= 2.0Install
npm install fastify-squirrellyUsage
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;
});Options
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
Examples
Hello World Example
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
Install fastify and fastify-squirrelly
npm install fastify fastify-squirrellyCreate the Fastify server
// 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 the Squirrelly template file
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>start the server!
node server.jsOpen 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.