Skip to content

Commit

Permalink
update readme, test, remove unused hoek dep
Browse files Browse the repository at this point in the history
  • Loading branch information
orthagonal committed Dec 8, 2017
1 parent cb8f2f6 commit 5e443b1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 38 deletions.
79 changes: 44 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,58 @@
[![Build Status](https://travis-ci.org/firstandthird/hapi-views.svg?branch=master)](https://travis-ci.org/firstandthird/hapi-views)
[![Coverage Status](https://coveralls.io/repos/github/firstandthird/hapi-views/badge.svg?branch=master)](https://coveralls.io/github/firstandthird/hapi-views?branch=master)

`hapi-views` is a hapi plugin that makes it very easy to register routes for views and pull in data. Great for pages that just need either some static data or data from an api.

`hapi-views` is a hapi plugin that lets you quickly and easily create your view routes. You use JSON to specify routes that can pull in dynamic data from any source, then use that data to render an HTML page.
## Features

* define routes from config
* pull in yaml data and pass it to the views
* pull in data from an api endpoint
* pull in data from a server method
* works with any Hapi view engine
* define view routes with JSON instead of programming route handlers
* specify dynamic JSON for your views with [varson](https://github.com/firstandthird/varson)
* pull in data with Hapi server methods

## Usage

```javascript
server.register({
register: require('hapi-views'),
await server.register({
plugin: require('hapi-views'),
options: {
dataPath: __dirname + '/public/pages/',
views: {
'/': {
view: 'landing/view',
yaml: 'landing/data.yaml'
},
'/comments/{id}': {
view: 'landing/view',
api: 'http://jsonplaceholder.typicode.com/comments/{params.id}'
},
'/load': {
view: 'landing/view',
method: 'serverLoad',
routeConfig: { // Any valid hapi route config
plugins: {
'hapi-hello-world': {}
}
}
}
},
// each type defined under 'globals' is loaded for all views
// 'globals' (optional) specifies global data that is common to all views
// data.serverName and data.db will be available to the view engine:
globals: {
yaml: 'global.yaml',
api: 'http://globalBargle.com/icons/{params.id}',
method: 'globalMethod',
inject: '/globals'
},
serverName: 'Bob the Server',
db: 'http://55.55.55.5555:2121/bobDb'
}
// "routes" can contain as many view routes as you like
// the key is the URL and the value is an object that specifies how to process the view for that URL
routes: {
// the URL for this route will be 'http://yourhost.com/homepage/{userId}'
'/homepage/{userId}': {
// 'view' (required) tells the view engine what HTML template to use:
view: 'homepage',
// 'data' (required) tells the view engine what context data to use when rendering the HTML
// this data is itself processed by the template engine in varson:
data: {
// an example of a literal string value:
title: "Your Homepage",
// varson evaluates string values inside the double-bracket '{{' delimiters as Javascript.
// So the view engine will see data.amount as 35:
amount: "{{ 15 + 25 }}",
// the Hapi request object (https://hapijs.com/api#request) can be referenced directly:
userId: "{{request.params.userId}}",
// Hapi server methods (https://hapijs.com/api#server.method()) can be referenced as 'methods'.
// for example, this expression will set data.userInfo to the value returned by calling server.methods.getUserInfo. Works for methods that return a promise as well:
userInfo: "{{methods.getUserInfo(request.params.userId)}}"
},
// 'preProcess' (optional) will be called before the request is processed:
preProcess: (request, options, h) => { }
// 'preResponse (optional) will be called after the request is processed but before the view is returned:
preResponse: (request, options, h) => { processRan = true; }
// 'onError' (optional) will be called if there is an error processing your data or view.
// The value returned by onError will be the result your route returns to the client:
onError: (err, h) => { return boom.badImplementation('this was an error') }
}
}
}
}, function(err) {
});
```

See [test/test.global.js](https://github.com/firstandthird/hapi-views/blob/master/test/test.globals.js) for working examples.
1 change: 0 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';
/* eslint new-cap: 0 */
const hoek = require('hoek');
const aug = require('aug');
const renderHandler = require('./lib/handler.js');
const defaults = {
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"lint": "eslint .",
"test": "lab -t 100 -v",
"test": "lab --leaks",
"test-travis": "lab -r lcov | ./node_modules/.bin/coveralls"
},
"repository": {
Expand All @@ -26,7 +26,6 @@
"dependencies": {
"aug": "^2.0.0",
"boom": "^6.0.0",
"hoek": "^5.0.0",
"p-props": "^1.1.0",
"varson": "^2.0.2"
},
Expand Down

0 comments on commit 5e443b1

Please sign in to comment.