Giter Club home page Giter Club logo

hapi-cms's Introduction

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

hapi-cms's People

Contributors

orthagonal avatar jgallen23 avatar ecwillis avatar dawnerd avatar antonio-laguna avatar

Watchers

 avatar  avatar James Cloos avatar Lara Chelak avatar Justin Blaisdell avatar Joe avatar Newton avatar  avatar  avatar  avatar

Forkers

isabella232

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.