Handlebars helpers which implement layout blocks similar to Jade, Jinja, Swig, and Twig.
With Node.js:
$ npm install handlebars-layouts
With Bower:
$ bower install shannonmoeller/handlebars-layouts
partial
String
- Name of partial to render.attributes
Object
(Optional) - Arbitrary values that will be added to the partial data context.
Loads a layout partial of a given name and defines block content.
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:
class Page extends Layout {
constructor() {
this.foo = 'bar';
}
title() {
return 'Example - ' + super();
}
}
partial
String
- Name of partial to render.attributes
Object
(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.
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:
class 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();
}
}
name
String
- 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.
Default block content is optional, and may be omitted.
name
String
- Block identifier.
Checks if a named block exists. Allows additional markup to be used conditionally in layouts, dependant on whether the {{#content [name]}}
is included on the page.
Can be nested and combined via the ||
separator:
name
String
- Identifier of the block to modify.mode
String
(Optional) - Means of providing block content. Default:replace
.
Sets block content, optionally appending or prepending using the mode
attribute.
Layout:
Page:
Output:
Content is optional, and may be omitted. This will cause the main
block to be replaced with an empty string, clearing out any default content.
Helpers are registered by passing in your instance of Handlebars. This allows you to selectively register the helpers on various instances of Handlebars.
handlebars
Handlebars
- An instance of Handlebars.
var handlebars = require('handlebars'),
layouts = require('handlebars-layouts');
layouts(handlebars);
handlebars
Handlebars
- An instance of Handlebars.
Helpers are also exposed via a register
method for use with Assemble.
var handlebars = require('handlebars'),
layouts = require('handlebars-layouts');
layouts.register(handlebars);
// or
grunt.initConfig({
assemble: {
options: {
helpers: ['path/to/handlebars-layouts.js']
}
}
});
// Load Handlebars
var Handlebars = require('handlebars');
// Register helpers
require('handlebars-layouts')(Handlebars);
// Register partials
Handlebars.registerPartial('layout', fs.readFileSync('layout.html', 'utf8'));
// Compile template
var template = Handlebars.compile(fs.readFileSync('template.html', 'utf8'));
// Render template
var output = template({
title: 'Layout Test',
items: [
'apple',
'orange',
'banana'
]
});
console.log(output);
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.
$ npm test
© 2015 Shannon Moeller me@shannonmoeller.com
Licensed under MIT