2.2.2 • Published 4 years ago

hapi-cms v2.2.2

Weekly downloads
11
License
MIT
Repository
github
Last release
4 years ago

hapi-cms

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.

Installation

npm install hapi-cms

Usage

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.

Example

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 )

Options

  • 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()); }
2.2.2

4 years ago

2.2.0

6 years ago

2.1.0

6 years ago

2.0.0

6 years ago

1.1.0

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago