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
- static
- inner
- ~viewOrchestra() ⇒
Promise
- ~viewOrchestra() ⇒
handler.plugin
Hapi plugin options
options
Object with the following keystemplates
{array} List with available templates. Multiple templates having the samename
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 folderactions
{object} Actions that are always evokedtemplate_id
{string} Template it should add parameters tocallback(params, h)
{async function}params
{object} Template parameter objecth
{object} hapi response toolkit object
Kind: static property of handler
Promise
handler~viewOrchestra() ⇒ Create a view by composing files in the templates
directory
through the director.html
options
{object} with the following keyscallbacks
{array} Array withasync function(params)
. The h toolkit instance is available as this e.g. you have request atthis.request
and server atthis.request.server
)[]( params, h)
{async function}params
{object} object holding params used at template compilationh
{object} hapi response toolkit object
director
{string} id of director to useinclude
{string} Template ids to includeparams
Object{key:values}
made available to templates forhandlebars.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.