npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hapi-view-context

v5.0.2

Published

Loads data into hapi views

Downloads

925

Readme

hapi-view-context Build Status

Helpers to automate the task of loading data into view rendering contexts. Keeps your route handlers clean!

Installation

npm install hapi-view-context

Usage

hapi-view-context lets you set up data in the current view rendering context in one of four ways:

  1. pass static global-level default key:value pairs (in the plugin options context field) that will be added to all contexts:
{
  context: {
    siteVersion: '2.4.3',
    copyright: 'Copyright (c) 2016 by You'
  }
}
  1. pass a global-level context-handling function or server method (in the plugin options contextHandler field), that will be invoked whenever 'onPostHandler' is called:
{
  contextHandler: (context, request) => {
    context.id = request.id;
    return context;
  }
}
  1. set a global-level context-handling function with server.setViewContext, the context-handler will then be invoked whenever 'onPostHandler' is called:
server.setViewContext( (context, request) => {
  context.id = request.id;
  return context;
});
  1. set request-level contexts inside route handlers and in response to request events with addContext.
server.addContext(request, 'id', request.id);

Example:

// register the plugin with a hapi server:
server.register({
  register: require('hapi-view-context'),
  options: {
    // static global-level default key:value pairs in 'context' will be added to all contexts:
    context: {
      'someVariable': 'Some Value',
      'some.nested.variable': 'Another value'
    },
    // a global-level context-handling function will be called on every request:
    contextHandler: (defaultContext, request) => {
      defaultContext.fromContextMethod = 'a value from the context method';
      return defaultContext;
    }
  }
});
// a context handler that will be able to modify the context for every request
server.setViewContext( (defaultContext, request) => {
  defaultContext.fromSetViewContext = 'a value from setViewContext';
  return defaultContext;
});
// a route that renders a view:
server.route({
  method: 'POST',
  path: '/',
  handler: (request, reply) => {
    // the route handler does not need to do anything else besides call the view renderer:
    reply.view('myView');
  }
});

Assuming that 'myView' is an HTML view that looks something like:

<h1> {{someVariable}}, {{some.nested.variable}}, {{fromContextMethod}}, {{fromSetViewContext}}.
</h1>

Then posting to the '/' route will render myView as the following:

<h1> Some Value, Another value, a value from the context method, a value from setViewContext.</h1>

We can add to the context for a specific request as well:

let calledOnce = false;
server.ext('onPostHandler', (request, reply) => {
  if (!calledOnce) {
    calledOnce = true;
    // addContext will only add to the context for the current request:
    server.plugins['hapi-view-context'].addContext(request, { someVariable: 'a totally different value' });
  }
  reply.continue();
});

Posting to '/' the first time will now return:

<h1> a totally different value, Another value, a value from the context method, a value from setViewContext.</h1>

But subsequent calls will not have the context that was added to the previous request:

<h1> Some Value, Another value, a value from the context method, a value from setViewContext.</h1>

See /test/test.context.js for more examples and usage.