hapi-orchestra-view

3.0.8 • Public • Published

Build Status Coverage Status

Hapi orchestra view

Library for composing handlebar html templates into a single page. With a director file you define the skeleton of a page and then it is composed from templates. First each template is compiled and then the director with the compiled templates as input. The function h.viewOrchestra(options) is used to returned the final html where options.params object holds the parameters available to all templates. Please se example below for more options.

Installation

Copy files in lib/views to preferred folder in project

Open the director.html and edit it accordingly to your preference. Each triple handlebar import corresponds to the compiled output (except for raw.html) of files in lib/view/templates.

Run npm install --save git+https://x-oauth-basic@github.com/mickelindahl/hapi_orchestra_view.git

Usage

'use strict'
 
const Hapi = require( 'hapi' );
 
const server = new Hapi.Server( { port: 3000 } );
 
server.register( {
   register: require( 'hapi-page-view' ),
   options:  {
      templates: [
         { id: 'my_head', param: 'head', path: 'orchestra/templates/head.html', compile:true },
         { id: 'my_body', param: 'body', path: 'main.html', compile:true },
         { id: 'my_raw', param: 'raw', path: 'orchestra/templates/raw.html', compile:false },
        ],
   directors: [{ id: 'my_director', path: 'orchestra/director.html' }],
   views: './lib/views',
   actions:[{
        template_id:'my_head',
        callbacks:[async (params)=>{params.page_title='Super site'}]
        }]
 
        }
    }).then(()=>{
 
    server.route([
        {
            method: 'GET',
            path: '/'
            handler: async (request, h)=>{
                return h.viewOrchestra({
                            director:'my_director',
                            include: ['my_head', 'my_body','my_body_2',  'my_raw', 'my_wrong'],
                            params: {title:'A title'},
                            callbacks:[async (params)=>{params.name='Humphrey Bogard'}]
                    }),
                }
            }
        ])
});

Methods

The handler is attached to hapijs server.methods

handler

handler.plugin

Hapi plugin options

  • options Object with the following keys
    • templates {array} List with available templates. Multiple templates having the same name will be merged
      • [].id {string} Template identifier
      • [].param {string} Director parameter name where template should be inserted
      • [].path {string} Path to template relative view directory
      • [].compile {boolean} Indicates weather template should be compiled or not
    • directors {array} List with available directors
      • [].id {string} Director identifier
      • [].path {string} Path to director
    • views {string} Path to view folder
    • actions {object} Actions that are always evoked
      • template_id {string} Template it should add parameters to
      • callback(params, h) {async function}
        • params {object} Template parameter object
        • h {object} hapi response toolkit object

Kind: static property of handler

handler~viewOrchestra() ⇒ Promise

Create a view by composing files in the templates directory through the director.html

  • options {object} with the following keys
    • callbacks {array} Array with async function(params). The h toolkit instance is available as this e.g. you have request at this.request and server at this.request.server)
      • []( params, h) {async function}
        • params {object} object holding params used at template compilation
        • h {object} hapi response toolkit object
    • director {string} id of director to use
    • include {string} Template ids to include
    • params Object {key:values} made available to templates for handlebars.compile

Kind: inner method of handler

Test

npm run-script test

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.

/hapi-orchestra-view/

    Package Sidebar

    Install

    npm i hapi-orchestra-view

    Weekly Downloads

    6

    Version

    3.0.8

    License

    MIT

    Unpacked Size

    175 kB

    Total Files

    19

    Last publish

    Collaborators

    • mikaellindahl