user379468 user379468 - 1 year ago 101
Jade Question

Is there a Handlbars equivalent to Jades extend

I'm contemplating moving the tempating language of my express app from Jade to Handlbars, and I'm wondering if there is an equivalent to the Jade extend directive in handlebars.

Answer Source

As i can see, in the repository of handlebars tells you that exist a dependencie for handlebars that can enable you to extends blocks. You can found more information here and here.


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

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

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

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

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

Here we define a basic layout where you can extend others html from this one.


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

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


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

In this file you set all the data you want to extend from layout.

The .js file

var handlebars = require('handlebars');
var layouts = require('handlebars-layouts');

// Register helpers 

// 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: [

1. Require handlebars and handlebars-layout
2. Register the helper in the handlebar as a layout.
3. Register partials set the file layout.hbs as a "module" called 'layout', then in the page.html you set the extension from 'layout' 4. Compile in template the extension page.html.
5. Render template passing data from js to the file.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download