{"_id":"@articulate/hapi-json-view","_rev":"220-a069216950651fb0d61d58fc5fef442b","name":"@articulate/hapi-json-view","description":"JSON view engine for hapi.","dist-tags":{"latest":"4.0.0"},"versions":{"4.0.0":{"name":"@articulate/hapi-json-view","version":"4.0.0","description":"JSON view engine for hapi.","main":"src/index.js","scripts":{"test":"lab --assert code --lint --threshold 100 --verbose"},"repository":{"type":"git","url":"git+https://github.com/articulate/hapi-json-view.git"},"keywords":["hapi","json","view"],"license":"MIT","bugs":{"url":"https://github.com/articulate/hapi-json-view/issues"},"homepage":"https://github.com/articulate/hapi-json-view","engines":{"node":">=4.0.0"},"peerDependencies":{"hapi":">=10.x.x","vision":">=3.x.x"},"devDependencies":{"code":"2.x.x","@nesive/eslint-config":"1.x.x","hapi":"11.x.x","lab":"7.x.x","vision":"3.x.x"},"gitHead":"8587780111352983a49b15241508f76ec6850d42","_id":"@articulate/hapi-json-view@4.0.0","_shasum":"0d096eb10bc3e31e38868869e27e32bc89942a84","_from":".","_npmVersion":"3.10.3","_nodeVersion":"6.3.1","_npmUser":{"name":"pklingem","email":"patrick.klingemann@gmail.com"},"dist":{"shasum":"0d096eb10bc3e31e38868869e27e32bc89942a84","tarball":"https://registry.npmjs.org/@articulate/hapi-json-view/-/hapi-json-view-4.0.0.tgz","integrity":"sha512-Y3kroh9CTaCc6ZHTK8d/LuFy2yousDuI03FrlVfNb/oA9rhxBph97+5fPw7U0od8DODw69Xzdsa2zapLEvEeCw==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIG6YV5Dz9llh4TpJawCE7ns9/GK6Y8R/6GOvbSf3aSkeAiA0a8KKvuNcn5Osbp1axUjQAzILLkBdG119b8VFN8XmoA=="}]},"maintainers":[{"name":"pklingem","email":"patrick.klingemann@gmail.com"}],"_npmOperationalInternal":{"host":"packages-18-east.internal.npmjs.com","tmp":"tmp/hapi-json-view-4.0.0.tgz_1488222735939_0.03741070185787976"},"directories":{}}},"readme":"# Hapi JSON View [![Build Status](https://travis-ci.org/nesive/hapi-json-view.svg)](https://travis-ci.org/nesive/hapi-json-view)\n\nA view engine for the hapi framework.\n\n## Installation\n\n```sh\nnpm install --save @nesive/hapi-json-view\n```\n\n## Usage\n\nserver.js:\n\n```js\nconst Hapi = require('hapi');\nconst HapiJsonView = require('@nesive/hapi-json-view');\nconst Path = require('path');\nconst Vision = require('vision');\n\nconst server = new Hapi.Server();\nserver.connection({ port: 8080 });\n\nserver.register(Vision, (err) => {\n  if (err) throw err;\n\n  server.views({\n    engines: {\n      js: {\n        module: HapiJsonView.create(),\n        contentType: 'application/json',\n      }\n    },\n    path: Path.join(__dirname, 'templates'),\n  });\n\n  server.route({\n    method: 'GET',\n    path: '/article',\n    handler: function(request, reply) {\n      const article = {\n        _id: '507f1f77bcf86cd799439011',\n        title: 'Node.js',\n        author: {\n          _id: '507f191e810c19729de860ea',\n          name: 'John Doe',\n        },\n      };\n\n      reply.view('article', { article: article });\n    },\n  });\n});\n```\n\ntemplates/article.js:\n\n```js\njson.set('title', article.title);\njson.set('author', (json) => {\n  json.set('name', article.author.name);\n});\n```\n\nThis template generates the following object:\n\n```js\n{\n  title: 'Node.js',\n  author: {\n    name: 'John Doe'\n  }\n}\n```\n\n## Functions\n\n### set()\n\nIt assigns a value to a key.\n\n```js\njson.set('title', 'Node.js');\n\n// => { title: 'Node.js' }\n```\n\nThe value can be a function. If `json.set()` is called with a key, it creates an object:\n\n```js\njson.set('author', (json) => {\n  json.set('name', 'John Doe');\n});\n\n// => { author: { name: 'John Doe' } }\n```\n\nIf `json.set()` is called without a key, it assign the value to the parent key:\n\n\n```js\njson.set('title', (json) => {\n  json.set('Node.js');\n});\n\n// => { title: 'Node.js' }\n```\n\n### array()\n\nIt creates a new array by iterating through an existing array:\n\n```js\nconst numbers = ['one', 'two'];\n\njson.set('numbers', json.array(numbers, (json, number) => {\n  json.set('number', number);\n}));\n\n// => { numbers: [{ number: 'one' }, { number: 'two' }] }\n```\n\n### extract()\n\nIt extracts values from an object and assigns them to the result object:\n\n```js\nconst numbers = { one: 'one', two: 'two', three: 'three' };\n\njson.extract(numbers, ['two', 'three']);\n\n// => { two: 'two', three: 'three' }\n```\n\n### helper()\n\nHelpers can be registered through the engine configuration:\n\n```js\nconst Hapi = require('hapi');\nconst HapiJsonView = require('@nesive/hapi-json-view');\nconst Path = require('path');\nconst Vision = require('vision');\n\nconst server = new Hapi.Server();\nserver.connection({ port: 8080 });\n\nserver.register(Vision, (err) => {\n  if (err) throw err;\n\n  server.views({\n    engines: {\n      js: {\n        module: HapiJsonView.create(),\n        contentType: 'application/json',\n      }\n    },\n    path: Path.join(__dirname, 'templates'),\n    helpersPath: Path.join(__dirname, 'templates/helpers'),\n  });\n});\n```\n\nThey can then be used by their name:\n\n```js\njson.set('title', json.helper('uppercase', article.title));\n```\n\n### partial()\n\nPartials can be registered through the engine configuration:\n\n```js\nconst Hapi = require('hapi');\nconst HapiJsonView = require('@nesive/hapi-json-view');\nconst Path = require('path');\nconst Vision = require('vision');\n\nconst server = new Hapi.Server();\nserver.connection({ port: 8080 });\n\nserver.register(Vision, (err) => {\n  if (err) throw err;\n\n  server.views({\n    engines: {\n      js: {\n        module: HapiJsonView.create(),\n        contentType: 'application/json',\n      }\n    },\n    path: Path.join(__dirname, 'templates'),\n    partialsPath: Path.join(__dirname, 'templates/partials'),\n  });\n});\n```\n\nThey can then be used by their name:\n\n```js\njson.set('author', json.partial('author', { author: article.author }));\n```\n","maintainers":[{"email":"platform@articulate.com","name":"botzo"}],"time":{"modified":"2024-04-24T15:52:33.550Z","created":"2017-02-27T19:12:16.579Z","4.0.0":"2017-02-27T19:12:16.579Z"},"homepage":"https://github.com/articulate/hapi-json-view","keywords":["hapi","json","view"],"repository":{"type":"git","url":"git+https://github.com/articulate/hapi-json-view.git"},"bugs":{"url":"https://github.com/articulate/hapi-json-view/issues"},"license":"MIT","readmeFilename":"README.md"}