0.1.0 • Published 1 year ago

govuk-prototype-filters v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

GOV.UK Prototype Filters · test

A collection of Nunjucks template filters to manipulate and transform:

Requirements

Node.js v16 or later.

Installation

npm install govuk-prototype-filters

Usage with the GOV.UK Prototype Kit

GOV.UK Prototype Filters are designed to work with the GOV.UK Prototype Kit.

If you are using v13 or later of the kit, the components will be immediately available once you have installed the package, and can be managed alongside other plugins in your prototype.

Advanced usage

govuk-prototype-filters exports an object containing all available filter functions. Using Nunjucks’ addFilter method you can add individual filters to your Nunjucks environment:

const { slugify } = require('govuk-prototype-filters')
const nunjucks = require('nunjucks')

const nunjucksEnv = nunjucks.configure(['./app/views'])

nunjucksEnv.addFilter("slugify", slugify)

If you are using an earlier version of the GOV.UK Prototype Kit, you can import all the filters into your /app/filters.js file, like so:

+ const prototypeFilters = require('govuk-prototype-filters');

  module.exports = function (env) {
    /**
     * Instantiate object used to store the methods registered as a
     * 'filter' (of the same name) within nunjucks. You can override
     * gov.uk core filters by creating filter methods of the same name.
     * @type {Object}
     */
-   var filters = {}
+   var filters = prototypeFilters

    // Existing filter
    filters.sayHi = function(name) {
        return 'Hi ' + name + '!'
    }

    return filters
  }