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-handlebars-master

v0.1.1

Published

Feed multiple partials (content pages) to a single master template

Downloads

6

Readme

Install

npm install gulp-handlebars-master --save-dev

What it does?

This lib aims to make compiling many pages which share the basic HTML structure more DRY. Based on gulp-compile-handlebars for compatibility with its options object format.

Why?

In the classic handlebars-based solution you can share headers, footers etc. but you have to include them as partials in every page, for example

index.hbs

<!doctype html>
<html>
  {{>head_section}}
<body>
  {{>navigation}}
  <p>Home page content goes here</p>
  <a href="about.html">{{ about.title }}</a>
  {{>footer}}
</body>
</html>

about.hbs

<!doctype html>
<html>
  {{>head_section}}
<body>
  {{>navigation}}
  <p>About us content goes here</p>
  <a href="index.html">{{ index.title }}</a>
  {{>footer}}
</body>
</html>

Using a "master" template you just create the content and it gets included in the master page. There are also some features related to the data object.

src/pages/index.hbs

<p>This is the index page</p>
<a href="about.html">{{ about.title }}</a>

src/pages/about.hbs

<p>This is about page</p>
<a href="index.html">{{ index.title }}</a>

src/master.hbs

<!doctype html>
<html>
  <head>
    <title>Handlebars Master Template - {{ _page.title }}</title>
  </head>
<body>

<header>
  <h1>Shared header - {{ _page.title }}</h1>
  <nav>
    <ul>
      <li class="{{ _page.nav_index }}">
        <a href="index.html">{{ index.title }}</a>
     </li>
     <li class="{{ _page.nav_about }}">
       <a href="about.html">{{ about.title }}</a>
     </li>
    </ul>
  </nav>
</header>

<main>
  {{{ content }}}
</main>

<footer>
  Shared footer
</footer>
</body>
</html>

gulp-handlebars-master uses file names to set a _page object with current page data for the master template. If a data object is passed to the task, everything under index key will be available as _page while compiling index.hbs, everything under about will be available as _page while compiling about.html etc. The whole data object is available to all the page templates and the master. This feature can be used to solve the "add a class to the current page in navigation" problem.

Compiling using gulp

gulpfile.js

var hbsmaster = require('gulp-handlebars-master');
var rename = require('gulp-rename');

gulp.task('handlebars', function() {

    var tepmplatedata = {
	  "index" : {
	    "title" : "Home page",
	    "nav_index" : "active"
	  },
	  "about" : {
	    "title" : "About us",
	    "nav_about" : "active"
	  }
    };
    
    gulp.src('./src/pages/*.hbs')
	  .pipe( hbsmaster('./src/master.hbs', templatedata, {}))
	  .pipe( rename( function(path){
	    path.extname = '.html';
	  }))
	  .pipe(gulp.dest('./dist'));
});

The first argument to the .pipe( hbsmaster('./src/master.hbs', templatedata, {})) is location of the master template, 2nd and 3rd arguments are data and options compatible with the arguments you can pass to gulp-compile-handlebars

Check out (a bit more advanced) example:

$ git clone https://github.com/pawelk/gulp-handlebars-master.git ./hbs-master
$ cd hbs-master/example
$ npm install
$ gulp