{"id":24383461,"url":"https://github.com/firstandthird/hapi-view-context","last_synced_at":"2025-10-30T19:40:48.048Z","repository":{"id":54281983,"uuid":"43395472","full_name":"firstandthird/hapi-view-context","owner":"firstandthird","description":"Sets variables for use in views","archived":false,"fork":false,"pushed_at":"2021-02-26T20:30:29.000Z","size":46,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-03-04T09:40:25.823Z","etag":null,"topics":["hapi-plugin","hapi-v17"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/firstandthird.png","metadata":{"files":{"readme":"readme.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-09-29T21:27:37.000Z","updated_at":"2022-04-07T18:49:29.000Z","dependencies_parsed_at":"2022-08-13T10:50:53.563Z","dependency_job_id":null,"html_url":"https://github.com/firstandthird/hapi-view-context","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/firstandthird%2Fhapi-view-context","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/firstandthird%2Fhapi-view-context/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/firstandthird%2Fhapi-view-context/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/firstandthird%2Fhapi-view-context/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/firstandthird","download_url":"https://codeload.github.com/firstandthird/hapi-view-context/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243259026,"owners_count":20262381,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hapi-plugin","hapi-v17"],"created_at":"2025-01-19T10:14:32.994Z","updated_at":"2025-10-30T19:40:47.979Z","avatar_url":"https://github.com/firstandthird.png","language":"JavaScript","readme":"# hapi-view-context [![Build Status](https://travis-ci.org/firstandthird/hapi-view-context.svg?branch=master)](https://travis-ci.org/firstandthird/hapi-view-context)\n\nHelpers to automate the task of loading data into view rendering contexts.  Keeps your route handlers clean!\n\n### Installation\n\n`npm install hapi-view-context`\n\n### Usage\n\nhapi-view-context lets you set up data in the current view rendering context in one of four ways:\n\n1. pass static global-level default key:value pairs (in the plugin options _context_ field) that will be added to all contexts:\n```js\nconst viewContextPlugin = require('hapi-view-context');\nawait server.register({\n  plugin: viewContext,\n  options: {\n    context: {\n      siteVersion: '2.4.3',\n      copyright: 'Copyright (c) 2021 by You'\n    }\n  }\n});\n```\n\n_siteVersion_ and _copyright_ will now be available every time you render a view\n\n2. pass a global-level context-handling function or server method (in the plugin options _contextHandler_ field), that will be invoked whenever 'onPostHandler' is called:\n```js\nawait server.register({\n  plugin: viewContext,\n  options: {\n    contextHandler: (context, request) =\u003e {\n      context.id = request.id;\n      context.time = new Date();\n      context.user = await request.server.users.find({ _id: context.userId });\n      return context;\n    }\n  }\n});\n```\n\nThis function is re-invoked every time you render a view, so it is very useful for when you have dynamic data that you want to make available for all of your views.\n\n3. set a global-level context-handling function directly on the server using _server.plugins['hapi-view-context'].setViewContext_.  Like the _contextHandler_ function above, the additional context-handler will then be invoked whenever 'onPostHandler' is called:\n```js\nserver.plugins['hapi-view-context'].setViewContext( (context, request) =\u003e {\n  context.id = request.id;\n  return context;\n});\n```\n\n4. set request-level contexts inside route handlers and in response to request events with _addContext_.\n```js\nserver.plugins['hapi-view-context].addContext(request, { id: request.id });\n```\n\n## Example:\n\n```js\n// register the plugin with a hapi server:\nserver.register({\n  register: require('hapi-view-context'),\n  options: {\n    // static global-level default key:value pairs in 'context' will be added to all contexts:\n    context: {\n      siteName: 'Zombo.Com',\n      clickText: 'Click Me!',\n      isAdmin: false\n    },\n    // a global-level context-handling function will be called on every request:\n    contextHandler(context, request) {\n      context.fromContextMethod = 'a value from the context method';\n      if (request.id) {\n        context.id = id;\n      }\n      if (request.state.session \u0026\u0026 request.state.session.isAdmin) {\n        context.isAdmin = true;\n      }\n      return context;\n    }\n  }\n});\n// a context handler that will be able to modify the context for every request\nserver.plugins['hapi-view-context'].setViewContext((defaultContext, request) =\u003e {\n  defaultContext.fromSetViewContext = 'a value from setViewContext';\n  return defaultContext;\n});\n// a route that renders a view:\nserver.route({\n  method: 'GET',\n  path: '/',\n  handler: (request, reply) =\u003e {\n    // the route handler does not need to do anything else besides call the view renderer:\n    reply.view('myView');\n  }\n});\n```\n\nAssuming that 'myView' is an HTML view that looks something like:\n```html\n\u003ch1\u003e{{siteName}}\u003c/h1\u003e\n\u003cbutton\u003e {{clickText}} \u003c/button\u003e\n{% if isAdmin %}\n\u003cbutton\u003eAdmin Menu \u003c/button\u003e\n{% endif %}\n```\n\nThen getting the '/' route will render myView as the following:\n```html\n\u003ch1\u003e Zombo.Com\u003c/h1\u003e\n\u003cbutton\u003e Click Me! \u003c/button\u003e\n```\n\nAnd getting it while logged in as an admin:\n```html\n\u003ch1\u003e Zombo.Com\u003c/h1\u003e\n\u003cbutton\u003e Click Me! \u003c/button\u003e\n\u003cbutton\u003e Admin Menu \u003c/button\u003e\n```\n\nWe can add to the context for a specific request as well:\n\n```js\nlet calledOnce = false;\nserver.ext('onPostHandler', (request, h) =\u003e {\n  // addContext will only add to the context for the current request:\n  if (!calledOnce) {\n    server.plugins['hapi-view-context'].addContext(request, { clickText: 'Welcome Back' });\n  }\n  calledOnce = true;\n  return h.continue();\n});\n```\n\nFetching '/' the first time will now return:\n```html\n\u003ch1\u003e Zombo.Com\u003c/h1\u003e\n\u003cbutton\u003e Click Me! \u003c/button\u003e\n```\n\nBut subsequent calls will not have the context that was added to the previous request:\n```html\n\u003ch1\u003e Zombo.Com\u003c/h1\u003e\n\u003cbutton\u003e Welcome Back \u003c/button\u003e\n```\n\n## Options\n- __context__\n\n   Default context object for view handlers\n\n- __contextHandler__\n\n  Context-handling methods\n\n- __enableDebug__\n\n  Will log debug info whenever a context is loaded\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffirstandthird%2Fhapi-view-context","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffirstandthird%2Fhapi-view-context","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffirstandthird%2Fhapi-view-context/lists"}