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

wax-on

v1.2.2

Published

Add support to Handlebars for template inheritance with the `block` and `extends` helpers.

Downloads

182

Readme

Wax On

Build Status NPM Dependency Status NPM Verion

Wax On adds support to Handlebars for template inheritance with the block and extends helpers.

Directly inspired by template Inheritance in Pug and works the same way in Handlebars.

Install

npm install --save wax-on

Usage

// app.js

const Handlebars = require("handlebars");
const wax = require("wax-on");

// register Wax On helpers with Handlebars
wax.on(Handlebars);
wax.setLayoutPath("/path/to/layouts");

Extends

The extends helper allows a template to extend a layout or parent template. It can then override certain pre-defined blocks of content.

Note: You can have multiple levels of inheritance, allowing you to create powerful hierarchies of templates.

Block

Handlebars blocks can provide default content if desired, however optional as shown below by block scripts, block content, and block foot.

{{! layout.hbs }}

<!DOCTYPE html>
<html lang="en">
<head>

	<meta charset="utf-8">
	<title>
		My Site — {{title}}
	</title>
	{{#block "scripts"}}
		<script src="/jquery.js"></script>
	{{/block}}

</head>
<body>

	{{#block "content"}}{{/block}}

	{{#block "foot"}}
		<div id="footer">
			<p>
				some footer content
			</p>
		</div>
	{{/block}}

</body>
</html>

Now to extend the layout, simply create a new file and use the extends helper as shown below, giving the path (server only). You may now define one or more blocks that will override the parent block content, note that here the foot block is not redefined and will output “some footer content”.

{{! page-a.hbs }}

{{#extends "layout"}}

	{{#block "scripts"}}
		<script src="/jquery.js"></script>
		<script src="/pets.js"></script>
	{{/block}}

	{{#block "content"}}
		<h1>
			{{title}}
		</h1>
		{{#each pets as |petName|}}
			<p>{{petName}}</p>
		{{/each}}
	{{/block}}

{{/extends}}

It’s also possible to override a block to provide additional blocks, as shown in the following example where the content block now exposes a sidebar and primary block for overriding, or the child template could override the content block all together.

{{! sub-layout.hbs }}

{{#extends "layout"}}

	{{#block "content"}}
		<div class="sidebar">
			{{#block "sidebar"}}
				<p>nothing</p>
			{{/block}}
		</div>
		<div class="primary">
			{{#block "primary"}}
				<p>nothing</p>
			{{/block}}
		</div>
	{{/block}}

{{/extends}}
{{! page-b.hbs }}

{{#extends "sub-layout"}}

	{{#block "content"}}
		<div class="sidebar">
			{{#block "sidebar"}}
				<p>nothing</p>
			{{/block}}
		</div>
		<div class="primary">
			{{#block "primary"}}
				<p>nothing</p>
			{{/block}}
		</div>
	{{/block}}

{{/extends}}

Block Append / Prepend

The block helper also allows you to prepend or append blocks in addition to the default behavior of replacing blocks. Suppose for example you have default scripts in a head block that you wish to utilize on every page, you might do this:

{{! layout.hbs }}

<!DOCTYPE html>
<html lang="en">
<head>

	{{#block "head"}}
		<script src="/vendor/jquery.js"></script>
		<script src="/vendor/caustic.js"></script>
	{{/block}}
	
</head>
<body>

	{{#block "content"}}{{/block}}
	
</body>
</html>
{{! page.hbs }}

{{#extends "layout"}}

	{{#block "head" mode="append"}}
		<script src="/vendor/three.js"></script>
		<script src="/game.js"></script>
	{{/block}}

{{/extends}}

The append and prepend helpers make this common use case even easier.

{{! page.hbs }}

{{#extends "layout"}}

	{{#append "head"}}
		<script src="/vendor/three.js"></script>
		<script src="/game.js"></script>
	{{/append}}

{{/extends}}

Todo

In chronological order (most recent first); not in order of priority. Please create an issue if you'd like any of these changes or to recommend other changes.

  • add support for running in the browser (client-side)
  • investigate possibility of using partial blocks in addition to the extends helper

Change Log

The Wax On change log can be found in the CHANGELOG.md file within the project.

License

Wax On is available under the MIT License.