An open API service indexing awesome lists of open source software.

https://github.com/firstandthird/hapi-nunjucks-helpers

Helpers for clientkit
https://github.com/firstandthird/hapi-nunjucks-helpers

hapi-plugin hapi-v17

Last synced: 8 months ago
JSON representation

Helpers for clientkit

Awesome Lists containing this project

README

          

# hapi-nunjucks-helpers

A library of useful [nunjucks](https://mozilla.github.io/nunjucks/) helpers and filters, with a wrapper for use with [hapi](https://hapi.dev/)

## Installation

```
npm install hapi-nunjucks-helpers
```

## Usage

hapi-nunjucks-helpers is designed to work with [vision](https://github.com/hapijs/vision) and
[vision-nunjucks](https://github.com/firstandthird/vision-nunjucks), you just need to make sure to tell nunjucks to compile the helpers in 'async' mode:

```js
await server.register(require('vision'));

server.views({
engines: {
njk: require('vision-nunjucks')
},
path: '/views',
isCached: false,
compileMode: 'async'
});

await server.register(require('hapi-nunjucks-helpers'));
```

Is all you need to do, all of the following helpers will then be available for use in your views:

## Asset Helper

When you register the plugin you can pass options to configure how your assets filter will work:

```js
await server.register({
plugin: require('hapi-nunjucks-helpers'),
options: {
assets: {
endpoint: 'http://localhost:8080/',
cdn: 'http://localhost',
mappingFile: 'assets-map.json'
}
}
});
```
These options are as follows:

- `endpoint` The base asset directory, default is none.

- `dist` The root directory under _endpoint_ where compiled asset files reside, so `assets` will look for compiled asset files in the directory specified by _${endpoint}/${dist}_. Default is none.

- `mappingFile` A JSON file containing the map of source files to their corresponding hashed assets, this file should look like:
```JS
{
"common.js": "common.0d5714f897be66e21c18.js",
"common.css": "common.f7f029f8ee35aec7a7f6.css",
"script.js": "script.68e5e334aa4dcb31eece.js"
...
}
```
- `cdn` - Optional CDN domain for assets. Default: none.
- `cache` - If `true`, caches contents of `mappingFile`. Default: `false` (you will generally want to leave cacheing off during development)

## Filters:

**asset**

Reads from the mapping file if available:

```html

```

Also possible to pass a version to force new fetch from cache:

```html