Handlebars

Component ID

2302007

Component name

Handlebars

Component type

module

Maintenance status

Development status

Component security advisory coverage

not-covered

Downloads

548

Component created

Component changed

Component body

Introduction

I know what you're thinking, "oh another JS library module, a glorified library hook. Whoopty doo.". Before you close the browser tab, give me a chance.

Handlebars.js is a great library that has made writing JS+HTML a breeze. That in and of itself, has nothing to do with Drupal really. What does? Well the one thing I wish was easier about Handlebars.js is adding includes to your JS files - that's where Drupal comes in.

Instructions

Create your code in a [file name].tpl.hbr file.
From your code call one of the following:

handlebars_add_template(array(
  'template' => [file name],
  'path' => drupal_get_path('module', '[module name]'),
));
$form['element']['#attached']['hbr'] = (
  'template' => [file name],
  'path' => drupal_get_path('module', '[module name]'),
);
function modulename_handlebars() {
  return array(
    array(
      'template' => [file name],
      'path' => drupal_get_path('module', '[module name]'),
    ),
  );
}

Et voila! Then just go about your regular JS and call that particular template with the corresponding ID. Some more potential features:

  • Advanced debugging settings
  • Theme suggestions
  • Place handlebars script tags in header or footer.
  • Functionality for template "partials"