A hapi plugin that simplifies rendering views with hapi server. You provide your own custom getPage
function to handle fetching the template page content from your preferred repository and hapi-cms will handle setting up the render vars for the page, validating the data and rendering it all as a view.
npm install hapi-cms
hapi-cms works by registering a route in hapi at /{slug*}
that handles fetching and validating view data and rendering the view, the only thing you have to do to get it working is register your preferred template rendering engine with hapi and then define your getPage() function
.
The function takes in getPage(slug, request, h)
and needs to
return an object of the form:
{
"_template": "<h1> Hello {{ aTemplateVariable }}!</h1>",
"aTemplateVariable": "some value",
"anotherTemplateVariable": "some other value"
}
_template should contain the template to be rendered, templates can be in whatever format is used by your view rendering engine
The remainining fields contain the data that your template will use for rendering and can be whatever you want.
Code:
const hapiCms = require('hapi-cms');
const server = new Hapi.Server({});
await server.register({
hapiCms,
options: {
routePrefix: '/render',
async getPage(slug) {
return {
_template: '{{slug}} {{key1}}!',
key1: 'World'
};
}
}
});
await server.start();
Then get http://localhost:8080/render/Hello
and you will get a page back: 'Hello World!'
getPage is an async function, so you can add whatever functionality you want (fetch a template, fetch a )
-
getPage(slug, request, h) (required)
A user-defined function that takes in a page slug and handles fetching the appropriate content data for that page.
-
dataKey
By default hapi-cms stores your template variables at the top level along with the reserved _template field. Specifying a dataKey option will cause hapi-cms to store the template variables inside a sub-object under that key. For example if you specify
dataKey: vars
then your data will be presented to the view rendering engine in the form"_template": "<h1> my template {{vars.variable1}} </h1>", "vars": { "variable1": "a var", "variable2": "another var" }
-
routePrefix
You can specify your own root for the CMS route. For example if routePrefix is 'cms' then your CMS route will be at
/cms/{slug*}
-
routeConfig
any additional route config you want for the CMS route
-
globalData
any global data to always merge with the template variables that were fetched
-
validateData(data)
override to validate the resulting data before using it in the _template, the default validation function is
validateData(date) { return Joi.validate(data, Joi.object()); }