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

hogan-middleware

v0.2.2

Published

Middleware component to use Hogan.js mustache templates as views in an Express server

Downloads

80

Readme

hogan-middleware

Middleware component to use Hogan.js mustache templates as views in an Express server

Usage

var app = express();

app.configure(function () {
  app.set('views', __dirname + '/views'); // tell express which directory your views are in
  app.set('view engine', 'mustache');     // name your templates
  app.engine('mustache', require('hogan-middleware').__express); // register the engine
});

Once registered, your routing in express can use a mustache file name as the view to be rendered:

app.get('/', req, res, next) {
  res.render('home', { SiteName: 'My Website' });
}

In this case there is a file named home.mustache in the views directory that may have content as:

<!doctype html>
<html>
  <head><title>Hello World</title></head>
  <body>
    <h1>{{SiteName}}</h1>
  </body>
</html>

Configuration

Optional functionality in the middleware can be set before passing it into express:

var hoganMiddleware = require('hogan-middleware');
hoganMiddleware({
   filter: ['**.mustache'],   // override the default file extension searched for
                              // default is just the mustache file extension

   flatten: true,             // make all partials available with just their file name
                              // rather than the slash delimited path. default is enabled

   watch: true                // set to false to remove the live updating watchers -
                              // can be useful for running in production where files
                              // will not be regularly changing.
});

app.engine('mustache', hoganMiddleware.__express);

Partial Templates

Mustache allows the use of partial templates, this is supported by the middleware component by making all templates available as partial templates when rendering a template.

When home.mustache is being used as the name of the template to be rendered, that can include a.mustache from the views directory by adding {{>a}}. As a.mustache is rendered as a partial, that also has all templates available to it for use as partials, so could in turn have {{>b}} to include a nested partial.

To allow for a tidy source tree, templates can be in any number of sub-directories under the main views directory, they are all made available for use as partials without any path identifier.

Note - multiple templates with the same name but in different directories will overwrite each other. Set the flatten configuration option to false to always use the relative path as the name of the partials (ie: {{>app/header}} instead of just {{>header}}). Whether the flatten option is enabled or not, the relative path name will always be available.

Note - don't include the same template as a partial inside itself.

Live Updating

As express uses the the render engine for the first time, a series of watches are added to any sub-directory of the views directory so that any changes are automatically reloaded for you while the server is still running.