From aaaff46a1324eecd7bb2d6a65ec2916e38a1576a Mon Sep 17 00:00:00 2001 From: Vlad Sirenko Date: Tue, 19 Sep 2023 15:13:15 +0200 Subject: [PATCH] add parse raw template in view (#391) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add parse raw template in view * Update index.js Co-authored-by: Gürgün Dayıoğlu * add doc --------- Co-authored-by: Gürgün Dayıoğlu --- .taprc | 1 - README.md | 328 ++++++++++++++++++++++++++++++++- index.js | 375 +++++++++++++++++++++++++------------- test/helper.js | 36 ++++ test/test-art-template.js | 106 +++++++++++ test/test-dot.js | 70 ++++++- test/test-ejs-async.js | 122 +++++++++++++ test/test-ejs.js | 66 +++++++ test/test-eta.js | 129 +++++++++++++ test/test-handlebars.js | 202 +++++++++++++++++++- test/test-liquid.js | 111 +++++++++++ test/test-mustache.js | 67 +++++++ test/test-nunjucks.js | 100 ++++++++++ test/test-pug.js | 66 +++++++ test/test-twig.js | 103 +++++++++++ 15 files changed, 1746 insertions(+), 136 deletions(-) diff --git a/.taprc b/.taprc index e205b5e8..27ed2557 100644 --- a/.taprc +++ b/.taprc @@ -3,7 +3,6 @@ jsx: false flow: false coverage: true nyc-arg: [--exclude=out] -check-coverage: false files: - test/**/*.js diff --git a/README.md b/README.md index dc712b56..f9862a16 100644 --- a/README.md +++ b/README.md @@ -284,6 +284,37 @@ To use partials in mustache you will need to pass the names and paths in the opt } ``` +```js +fastify.get('/', (req, reply) => { + reply.view('./templates/index.mustache', data) +}) +``` + +```js +fastify.get('/', (req, reply) => { + fs.readFile('./templates/index.mustache', 'utf8', (err, file) => { + if (err) { + reply.send(err) + } else { + const render = mustache.render.bind(mustache, file) + reply.view(render, data) + } + }) +}) +``` + +```js +fastify.get('/', (req, reply) => { + fs.readFile('./templates/index.mustache', 'utf8', (err, file) => { + if (err) { + reply.send(err) + } else { + reply.view({ raw: file }, data) + } + }) +}) +``` + ### Handlebars To use partials in handlebars you will need to pass the names and paths in the options parameter: @@ -330,6 +361,31 @@ fastify.get("/", (req, reply) => { }); ``` +```js +fastify.get('/', (req, reply) => { + fs.readFile('./templates/index.hbs', 'utf8', (err, file) => { + if (err) { + reply.send(err) + } else { + const render = handlebars.compile(file) + reply.view(render, data) + } + }) +}) +``` + +```js +fastify.get('/', (req, reply) => { + fs.readFile('./templates/index.hbs', 'utf8', (err, file) => { + if (err) { + reply.send(err) + } else { + reply.view({ raw: file }, data) + } + }) +}) +``` + ### Nunjucks You can load templates from multiple paths when using the nunjucks engine: @@ -356,6 +412,37 @@ options: { } ``` +```js +fastify.get('/', (req, reply) => { + reply.view('./templates/index.njk', data) +}) +``` + +```js +fastify.get('/', (req, reply) => { + fs.readFile('./templates/index.njk', 'utf8', (err, file) => { + if (err) { + reply.send(err) + } else { + const render = nunjucks.compile(file) + reply.view(render, data) + } + }) +}) +``` + +```js +fastify.get('/', (req, reply) => { + fs.readFile('./templates/index.njk', 'utf8', (err, file) => { + if (err) { + reply.send(err) + } else { + reply.view({ raw: file }, data) + } + }) +}) +``` + ### Liquid To configure liquid you need to pass the engine instance as engine option: @@ -380,6 +467,31 @@ fastify.get("/", (req, reply) => { }); ``` +```js +fastify.get('/', (req, reply) => { + fs.readFile('./templates/index.liquid', 'utf8', (err, file) => { + if (err) { + reply.send(err) + } else { + const render = engine.renderFile.bind(engine, './templates/index.liquid') + reply.view(render, data) + } + }) +}) +``` + +```js +fastify.get('/', (req, reply) => { + fs.readFile('./templates/index.liquid', 'utf8', (err, file) => { + if (err) { + reply.send(err) + } else { + reply.view({ raw: file }, data) + } + }) +}) +``` + ### doT When using [doT](https://github.com/olado/doT) the plugin compiles all templates when the application starts, this way all `.def` files are loaded and @@ -388,8 +500,6 @@ This behavior is recommended by the doT team [here](https://github.com/olado/doT To make it possible it is necessary to provide a `root` or `templates` option with the path to the template directory. ```js -const path = require('node:path'); - fastify.register(require("@fastify/view"), { engine: { dot: require("dot"), @@ -406,6 +516,220 @@ fastify.get("/", (req, reply) => { }); ``` +```js +const d = dot.process({ path: 'templates', destination: 'out' }) +fastify.get('/', (req, reply) => { + reply.view(d.index, data) +}) +``` + +```js +fastify.get('/', (req, reply) => { + reply.view({ raw: readFileSync('./templates/index.dot'), imports: { def: readFileSync('./templates/index.def') } }, data) +}) +``` + +### eta + +```js +const { Eta } = require('eta') +let eta = new Eta() +fastify.register(pointOfView, { + engine: { + eta + }, + templates: 'templates' +}) + +fastify.get("/", (req, reply) => { + reply.view("index.eta", { text: "text" }); +}); +``` + +```js +fastify.get('/', (req, reply) => { + fs.readFile('./templates/index.eta', 'utf8', (err, file) => { + if (err) { + reply.send(err) + } else { + reply.view(eta.compile(file), data) + } + }) +}) +``` + +```js +fastify.get('/', (req, reply) => { + fs.readFile('./templates/index.eta', 'utf8', (err, file) => { + if (err) { + reply.send(err) + } else { + reply.view({ raw: file }, data) + } + }) +}) +``` + +### ejs + +```js +const ejs = require('ejs') +fastify.register(pointOfView, { + engine: { + ejs + }, + templates: 'templates' +}) + +fastify.get("/", (req, reply) => { + reply.view("index.ejs", { text: "text" }); +}); +``` + +```js +fastify.get('/', (req, reply) => { + fs.readFile('./templates/index.ejs', 'utf8', (err, file) => { + if (err) { + reply.send(err) + } else { + reply.view(ejs.compile(file), data) + } + }) +}) +``` + +```js +fastify.get('/', (req, reply) => { + fs.readFile('./templates/index.ejs', 'utf8', (err, file) => { + if (err) { + reply.send(err) + } else { + reply.view({ raw: file }, data) + } + }) +}) +``` + +### pug + +```js +const pug = require('pug') +fastify.register(pointOfView, { + engine: { + pug + } +}) + + +fastify.get("/", (req, reply) => { + reply.view("index.pug", { text: "text" }); +}); +``` + +```js +fastify.get('/', (req, reply) => { + fs.readFile('./templates/index.pug', 'utf8', (err, file) => { + if (err) { + reply.send(err) + } else { + reply.view(pug.compile(file), data) + } + }) +}) +``` + +```js +fastify.get('/', (req, reply) => { + fs.readFile('./templates/index.pug', 'utf8', (err, file) => { + if (err) { + reply.send(err) + } else { + reply.view({ raw: file }, data) + } + }) +}) +``` + +### twig + +```js +const twig = require('twig') +fastify.register(pointOfView, { + engine: { + twig + } +}) + + +fastify.get("/", (req, reply) => { + reply.view("index.twig", { text: "text" }); +}); +``` + +```js +fastify.get('/', (req, reply) => { + fs.readFile('./templates/index.twig', 'utf8', (err, file) => { + if (err) { + reply.send(err) + } else { + reply.view(twig.twig({ data: file }), data) + } + }) +}) +``` + +```js +fastify.get('/', (req, reply) => { + fs.readFile('./templates/index.twig', 'utf8', (err, file) => { + if (err) { + reply.send(err) + } else { + reply.view({ raw: file }, data) + } + }) +}) +``` + +### art + +```js +const art = require('art-template') +fastify.register(pointOfView, { + engine: { + 'art-template': art + } +}) + + +fastify.get("/", (req, reply) => { + reply.view("./index.art", { text: "text" }); +}); +``` + +```js +fastify.get('/', (req, reply) => { + fs.readFile('./templates/index.art', 'utf8', (err, file) => { + if (err) { + reply.send(err) + } else { + reply.view(art.compile({ filename: path.join(__dirname, '..', 'templates', 'index.art') }), data) + } + }) +}) +``` + +```js +fastify.get('/', (req, reply) => { + fs.readFile('./templates/index.art', 'utf8', (err, file) => { + if (err) { + reply.send(err) + } else { + reply.view({ raw: file }, data) + } + }) +}) +``` +