Socket
Socket
Sign inDemoInstall

handlebars-layouts

Package Overview
Dependencies
0
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    handlebars-layouts

Handlebars helpers which implement Jade-like layout blocks.


Version published
Weekly downloads
98K
decreased by-15.5%
Maintainers
1
Install size
22.4 kB
Created
Weekly downloads
 

Package description

What is handlebars-layouts?

The handlebars-layouts npm package is an extension for Handlebars that provides layout and partial support, allowing you to create complex templates with reusable components and layouts. It simplifies the process of managing nested templates and layouts in Handlebars.

What are handlebars-layouts's main functionalities?

Define Layouts

This feature allows you to define layouts that can be extended by other templates. In this example, a base layout is extended, and content is inserted into the 'body' section.


const Handlebars = require('handlebars');
const layouts = require('handlebars-layouts');
Handlebars.registerHelper(layouts(Handlebars));

const source = `
{{#extend "base"}}
  {{#content "body"}}
    <h1>{{title}}</h1>
    <p>{{message}}</p>
  {{/content}}
{{/extend}}
`;

const template = Handlebars.compile(source);
const context = { title: 'Hello, World!', message: 'This is a message.' };
const result = template(context);
console.log(result);

Nested Layouts

This feature demonstrates how to use nested layouts. A base layout is extended by a child layout, which is further extended by a page template. This allows for complex and reusable template structures.


const Handlebars = require('handlebars');
const layouts = require('handlebars-layouts');
Handlebars.registerHelper(layouts(Handlebars));

const baseLayout = `
<!DOCTYPE html>
<html>
<head>
  <title>{{title}}</title>
</head>
<body>
  {{#block "body"}}{{/block}}
</body>
</html>
`;

const childLayout = `
{{#extend "base"}}
  {{#content "body"}}
    <div class="container">
      {{#block "content"}}{{/block}}
    </div>
  {{/content}}
{{/extend}}
`;

const pageTemplate = `
{{#extend "child"}}
  {{#content "content"}}
    <h1>{{title}}</h1>
    <p>{{message}}</p>
  {{/content}}
{{/extend}}
`;

Handlebars.registerPartial('base', baseLayout);
Handlebars.registerPartial('child', childLayout);

const template = Handlebars.compile(pageTemplate);
const context = { title: 'Nested Layouts', message: 'This is a nested layout example.' };
const result = template(context);
console.log(result);

Dynamic Partials

This feature allows for dynamic partials, where the partial to be included is determined at runtime. In this example, the partial name is looked up from the context and the corresponding partial is rendered.


const Handlebars = require('handlebars');
const layouts = require('handlebars-layouts');
Handlebars.registerHelper(layouts(Handlebars));

const source = `
{{#extend "base"}}
  {{#content "body"}}
    {{> (lookup . 'partialName') }}
  {{/content}}
{{/extend}}
`;

const baseLayout = `
<!DOCTYPE html>
<html>
<head>
  <title>{{title}}</title>
</head>
<body>
  {{#block "body"}}{{/block}}
</body>
</html>
`;

const partials = {
  partial1: '<p>This is partial 1</p>',
  partial2: '<p>This is partial 2</p>'
};

Handlebars.registerPartial('base', baseLayout);
Handlebars.registerPartial('partial1', partials.partial1);
Handlebars.registerPartial('partial2', partials.partial2);

const template = Handlebars.compile(source);
const context = { title: 'Dynamic Partials', partialName: 'partial1' };
const result = template(context);
console.log(result);

Other packages similar to handlebars-layouts

Changelog

Source

0.3.0 - 0.3.1

Features:

  • Refactor.
  • Switched from Grunt to Gulp.
  • Improved tests including coverage.

Readme

Source

Handlebars Layouts

Handlebars helpers which implement Jade-like layout blocks.

NPM version Build Status Coverage Status Dependency Status

Install

With Node.js:

$ npm install handlebars-layouts

With Bower:

$ bower install handlebars-layouts

With Component:

$ component install shannonmoeller/handlebars-layouts

Example

Layout Partial

<!doctype html>
<html lang="en-us">
<head>
    {{#block "head"}}
        <title>{{title}}</title>

        <link rel="stylesheet" href="assets/css/screen.css" />
    {{/block}}
</head>
<body>
    <div class="site">
        <div class="site-hd" role="banner">
            {{#block "header"}}
                <h1>{{title}}</h1>
            {{/block}}
        </div>

        <div class="site-bd" role="main">
            {{#block "body"}}
                <h2>Hello World</h2>
            {{/block}}
        </div>

        <div class="site-ft" role="contentinfo">
            {{#block "footer"}}
                <small>&copy; 2013</small>
            {{/block}}
        </div>
    </div>

    {{#block "foot"}}
        <script src="assets/js/controllers/home.js"></script>
    {{/block}}
</body>
</html>

Template

{{#extend "layout"}}
    {{#append "head"}}
        <link rel="stylesheet" href="assets/css/home.css" />
    {{/append}}

    {{#replace "body"}}
        <h2>Welcome Home</h2>

        <ul>
            {{#items}}
                <li>{{.}}</li>
            {{/items}}
        </ul>
    {{/replace}}

    {{#prepend "foot"}}
        <script src="assets/js/analytics.js"></script>
    {{/prepend}}
{{/extend}}

Putting Them Together

// 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', 'uft8'));

// Render template
var output = template({
    title: 'Layout Test',
    items: [
        'apple',
        'orange',
        'banana'
    ]
});

console.log(output);

Output (prettified for readability)

<!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>&copy; 2013</small>
        </div>
    </div>

    <script src="assets/js/analytics.js"></script>
    <script src="assets/js/controllers/home.js"></script>
</body>
</html>

Test

$ npm test

License

MIT

Keywords

FAQs

Last updated on 22 Apr 2014

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc