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

gulp-json-handlebars

v1.0.6

Published

Gulp plugin -- pipe json as template data for a Handlebars template

Downloads

3

Readme

gulp-json-handlebars

Another Gulp handlebars plugin that starts with the template data and ends with the html.

(Contrast this with gulp-handlebars-html which starts with the handlebars file and you add the template data if you want)

// foo.html.json
{
  "meta": {
    "pageTemplate": "bar"
  },
  "otherStuff": "123"
}
// bar.hbs
<html>
  <body>
    <div>Other stuff {{otherStuff}}</div>
  </body>
</html>
// gulpfile.js
const gulpJsonHandlebars = require('gulp-json-handlebars');
const extensionReplace = require('gulp-ext-replace');

const handlebarsOptions = {
  partialsDirectory: 'src/handlebars-partials'
}

gulp.task('build', () => {
  gulp
    .src('src/**/*.html.json')
    .pipe(gulpJsonHandlebars(handlebarsOptions, getPageTemplate))
    .pipe(extensionReplace(''))          // optional, renames *.html.json to *.html
    .pipe(gulp.dest('build'));
});

// returns handlebars template given a template name
// the template name comes from meta.pageTemplate property in the json file
// you decide how to pick the template
// example: you have a folder of templates and you load the file indicated by the template name
const getPageTemplate = pageTemplateName => {
    const templatePath = path.join(handlebarsDirectory, 'pages', pageTemplateName + '.hbs');
    return fs.readFileSync(templatePath).toString('utf-8');
};


// Alternative: minimal getPageTemplate
// just return a string
const minimalGetPageTemplate = () => `
  <html>
    <body>
      <div>Other stuff {{otherStuff}}</div>
    </body>
  <html>
`

handlebarsOptions

  • partialsDirectory -- string or array of strings indicating paths to handlebars partials
  • supplementaryData -- optional object, will be merged with the template data from the json that's piped through
  • preProcessData -- optional function which will be called with the data parsed from the json file. The return value from this function will be used instead of the json data.

Attribution

Thanks to gulp-handlebars-html, I adapted the partial registration stuff from there.

Contribution

PRs welcome, as long as they include appropriate tests and updates to the README.