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

@boris-turner/amphora-html

v4.2.1-cnn.0

Published

An HTML renderer for component data

Downloads

2

Readme

Amphora HTML

The HTML renderer for Clay components that use Handlebars templates.

Install

$ npm install --save amphora-html

The Why

HTML rendering was controlled entirely by Amphora in v2.x, which meant that Amphora was responsible for a lot of heavy lifting. Furthermore, rendering was handled by a module called Multiplex Templates which did some :crystal_ball: dark magic :sparkles: to synchronously render component templates. This required some affordances from Amphora which added to the weight of the middleware. By separating renderers out into separate modules which can be plugged into Amphora >v2.x we can create renderers for specific requirements (XML, Amp, etc.) and the rendering process can be moved to separate machines to create more maintainable systems.

Integration

Basic Configuration

First, ensure that you have a compatible version of Amphora installed (v3.x or greater) and require amphora-html at the from wherever you are running Amphora.

const amphoraHtml = require('amphora-html');

Second, register a rootPath with the renderer. This will allow the renderer to reference your components directory and static assets directory properly. Usually this is the root of your project, but that may not be the case for your implementation.

// Register a root path for Amphora HTML
amphoraHtml.addRootPath(path.dirname(path.resolve('./package.json')));

Handlebars Helpers

If your templates require any custom Handlebars Helpers you can register them with the renderer's Handlebars instance. Simply pass in an object whose keys are the names of your helpers and whose values are the helper themselves. Like so:

// My helpers
const helpers = {
  // set up handlebars helpers that rely on internal services
  'nameOfHelper': () => {
    // helper that does something you need.
    return 'foobar';
  }
};

// Register helpers
amphoraHtml.addHelpers(helpers);

Amphora HTML Plugins

Amphora HTML plugins let you read and modify both the data sent to Handlebars and the HTML returned by Handlebars. An Amphora HTML plugin is an object with a render function that returns a modified data object and/or a postRender function that returns the rendered HTML string:


module.exports.render = (ref, data, locals) => {
  // you have the option to mutate `data` here
  // do **not** attempt to mutate `locals`

  // return `data` or a promise for `data`
  return data;
};

module.exports.postRender = (ref, html, locals) => {
  // you have the option to mutate `html` here
  // do **not** attempt to mutate `locals`

  // return `html` or a promise for `html`
  return html;
};

You can add Amphora HTML plugins like this:

amphoraHtml.addPlugins([
  { render: plugin1 },
  { postRender: plugin2 }
]);

One use case for Amphora HTMK plugins is to skip rendering of certain components depending on query parameters, which are available in locals.query. Amphora HTML plugins do not run when rendering for edit mode.

Register Amphora HTML with your Amphora Instance

Now that you have registered any helpers and plugins and have provided a root path which Amphora HTML can work from, you can register your renderer with Amphora. Registering consists of providing a renderers object whose keys are the extension of an HTTP request and whose values are the renderer. You can also specify a default property whose value is the extension that Amphora should default to when rendering. This is handy for rendering routes who don't end in extensions, such as mycoolsite.com/about/.

return amphora({
  app: app,
  renderers: {
    html: amphoraHtml,
    default: 'html'
  },
  providers: ['apikey', amphoraProvider],
  sessionStore: redisStore,
  plugins: [
    amphoraSearch
  ]
});

Contributing

Want a feature or find a bug? Create an issue or a PR and someone will get on it.