Path: blob/main/examples/example-ejs-with-some-options.js
107 views
'use strict'12const fastify = require('fastify')()3const resolve = require('node:path').resolve4const templatesFolder = 'templates'5const data = { text: 'Hello from EJS Templates' }67fastify.register(require('..'), {8engine: {9ejs: require('ejs')10},11defaultContext: {12header: 'header value defined as default context',13footer: 'footer value defined as default context'14},15includeViewExtension: true,16layout: 'layout',17templates: templatesFolder,18options: {19filename: resolve(templatesFolder)20},21charset: 'utf-8' // sample usage, but specifying the same value already used as default22})2324fastify.get('/', (_req, reply) => {25// reply.type('text/html; charset=utf-8').view('index-linking-other-pages', data) // sample for specifying with type26reply.view('index-linking-other-pages', data)27})2829fastify.get('/include-test', (_req, reply) => {30reply.view('index-with-includes', data)31})3233fastify.get('/include-one-include-missing-test', (_req, reply) => {34reply.view('index-with-includes-one-missing', data)35})3637fastify.get('/include-one-attribute-missing-test', (_req, reply) => {38reply.view('index-with-includes-and-attribute-missing', data)39})4041fastify.listen({ port: 3000 }, err => {42if (err) throw err43console.log(`server listening on ${fastify.server.address().port}`)44})454647