Handlebars layouts
Handlebars helpers which implement layout blocks similar to Jinja, Nunjucks (Swig), Pug (Jade), and Twig.
[![NPM version][npm-img]][npm-url] [![Downloads][downloads-img]][npm-url] [![Build Status][travis-img]][travis-url] [![Coverage Status][coveralls-img]][coveralls-url] [![Tip][amazon-img]][amazon-url] The project is written primarily in JavaScript, distributed under the MIT License license, first published in 2013. Key topics include: blocks, embed, handlebars, layout, layouts.
handlebars-layouts
Handlebars helpers which implement layout blocks similar to Jade, Jinja, Nunjucks, Swig, and Twig.
Install
With Node.js:
$ npm install handlebars-layouts
With Bower:
$ bower install shannonmoeller/handlebars-layouts
API
Helpers are generated by passing in your instance of Handlebars. This allows you to selectively register the helpers on various instances of Handlebars.
layouts(handlebars) : Object
handlebarsHandlebars- An instance of Handlebars.
Generates an object containing the layout helpers suitible for passing into registerHelper.
jsvar handlebars = require('handlebars'), layouts = require('handlebars-layouts'); handlebars.registerHelper(layouts(handlebars));
layouts.register(handlebars) : Object
handlebarsHandlebars- An instance of Handlebars.
Both generates an object containing the layout helpers and registers them with Handlebars automatically.
jsvar handlebars = require('handlebars'), layouts = require('handlebars-layouts'); layouts.register(handlebars);
Helpers
{{#extend [partial] [context] [key=value ...]}}
partialString- Name of partial to render.contextObject(Optional) - A custom context for the partial.attributesObject(Optional) - Arbitrary values that will be added to the partial data context.
Loads a layout partial of a given name and defines block content.
handlebarsExample -
The {{#extend}} helper allows you to reason about your layouts as you would class extension where the above is equivalent to the following psuedo code:
jsclass Page extends Layout { constructor() { this.foo = 'bar'; } title() { return 'Example - ' + super(); } }
{{#embed [partial] [context] [key=value ...]}}
partialString- Name of partial to render.contextObject(Optional) - A custom context for the partial.attributesObject(Optional) - Arbitrary values that will be added to the partial data context.
Allows you to load a partial which itself extends from a layout. Blocks defined in embedded partials will not conflict with those in the primary layout.
handlebars<img src="1.png" alt="" /> <img src="2.png" alt="" /> Image 1 - <img src="1.png" alt="" />
The {{#embed}} helper allows you to reason about your partials as you would class instantiation where the above is equivalent to the following psuedo code:
jsclass Page extends Layout { body() { var gallery = new Gallery(); gallery.replaceBody('<img src="1.png" alt="" />\n<img src="2.png" alt="" />'); var modal = new Modal({ foo: 'bar', name: this.user.fullName }); modal.prependTitle('Image 1 - '); modal.replaceBody('<img src="1.png" alt="" />'); return gallery.toString() + modal.toString(); } }
{{#block [name]}}
nameString- Block identifier.
Defines a named block, with optional default content. Blocks may have content appended, prepended, or replaced entirely when extending or embedding. You may append and prepend to the same block multiple times.
handlebars<h1>Hello World</h1> <p>Lorem ipsum...</p> <p>© 1970</p>
{{#content [name] mode="(append|prepend|replace)"}}
nameString- Identifier of the block to modify.modeString(Optional) - Means of providing block content. Default:replace.
Sets block content, optionally appending or prepending using the mode attribute.
Layout:
handlebars<html> ... <body> <h1>Hello World</h1> <p>Lorem ipsum.</p> <p>© 1999</p> </body> </html>
Page:
handlebars<h1>Goodnight Moon</h1> <p>Dolor sit amet.</p> <p>MIT License</p>
Output:
handlebars<html> ... <body> <h1>Goodnight Moon</h1> <p>Lorem ipsum.</p> <p>Dolor sit amet.</p> <p>MIT License</p> <p>© 1999</p> </body> </html>
Conditional Blocks
There are times where you need to wrap a block with an element or use a different class depending on whether content has been provided for a block. For this purpose, the content helper may be called as a subexpression to check whether content has been provided for a block.
For example, you may wish to have an optional column in a grid layout:
handlebars<div class="grid"> <div class="grid-col grid-col_2of3grid-col_full"> </div> <div class="grid-col grid-col_1of3"> </div> </div>
For a page that only needs a left column, you may omit defining content for the right block:
handlebars<p>Left</p>
Resulting in:
html<div class="grid"> <div class="grid-col grid-col_full"> <p>Left</p> </div> </div>
For a page with two columns, simply define content for both blocks:
handlebars<p>Left</p> <p>Right</p>
Resulting in:
html<div class="grid"> <div class="grid-col grid-col_2of3"> <p>Left</p> </div> <div class="grid-col grid-col_1of3"> <p>Right</p> </div> </div>
Example
layout.hbs
handlebars<!doctype html> <html lang="en-us"> <head> <title></title> <link rel="stylesheet" href="assets/css/screen.css" /> </head> <body> <div class="site"> <div class="site-hd" role="banner"> <h1></h1> </div> <div class="site-bd" role="main"> <h2>Hello World</h2> </div> <div class="site-ft" role="contentinfo"> <small>© 2013</small> </div> </div> <script src="assets/js/controllers/home.js"></script> </body> </html>
page.html
handlebars<link rel="stylesheet" href="assets/css/home.css" /> <h2>Welcome Home</h2> <ul> <li></li> </ul> <script src="assets/js/analytics.js"></script>
Putting Them Together
jsvar handlebars = require('handlebars'); var layouts = require('handlebars-layouts'); // Register helpers handlebars.registerHelper(layouts(handlebars)); // Register partials handlebars.registerPartial('layout', fs.readFileSync('layout.hbs', 'utf8')); // Compile template var template = handlebars.compile(fs.readFileSync('page.html', 'utf8')); // Render template var output = template({ title: 'Layout Test', items: [ 'apple', 'orange', 'banana' ] }); console.log(output);
Output (prettified for readability)
handlebars<!doctype html> <html lang="en-us"> <head> <title>Layout Test</title> <link rel="stylesheet" href="assets/css/screen.css" /> <link rel="stylesheet" href="assets/css/home.css" /> </head> <body> <div class="site"> <div class="site-hd" role="banner"> <h1>Layout Test</h1> </div> <div class="site-bd" role="main"> <h2>Welcome Home</h2> <ul> <li>apple</li> <li>orange</li> <li>banana</li> </ul> </div> <div class="site-ft" role="contentinfo"> <small>© 2013</small> </div> </div> <script src="assets/js/analytics.js"></script> <script src="assets/js/controllers/home.js"></script> </body> </html>
Contribute
Standards for this project, including tests, code coverage, and semantics are enforced with a build tool. Pull requests must include passing tests with 100% code coverage and no linting errors.
Test
$ npm test
MIT © Shannon Moeller
Contributors
Showing top 4 contributors by commit count.
