panthera-view-engine
Handlebars.js-based view engine middleware for Koa applications.
Intended for use with any kind of database, provided the database tables or collections are organized compatibly and the data is retrieved in a way the view engine is capable of consuming. Introduces the concept of themes, or numerically-identified sets of templates that include any number of layouts, any number of views, and any number of partials.
See test/integration/setup.sql for a compatible MySQL table structure.
Installation
npm install panthera-view-engine
Usage
const koa = ;const viewEngine = ; let app = ;// app.context.models = ...;app; app; app;
models
The models must be attached to the Koa application context. app.context.models
is expected to be an object with properties named layouts
, partials
, and views
. Each of these is expected to implement a method that retrieves the necessary data and returns a Promise object. The resolution values of the returned Promises must be compatible with results returned from mysql
queries.
The implementation that panthera-view-engine
expects is the following.
let models = layouts: /** * @param * @param * @param * @param * @return */ { /* return Promise.resolve([ [ { contents: '' } ] ]); */ } partials: /** * @param * @param * @param * @return */ { /* return Promise.resolve([ [ { name: '', contents: '' }, { name: '', contents: '' } ] ]); */ } views: /** * @param * @param * @param * @param * @return */ { /* return Promise.resolve([ [ { contents: '' } ] ]); */ } ;
Rendering options
By default, panthera-view-engine
renders templates from themes for the namespace __default
, using the theme with the id 1
, falling back on the layout named main
.
These options may be changed by passing in an options argument with any of the appropriate properties as a third argument:
app;