Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the differences between the Handlebars 1.x and 2.x APIs?

We are currently using Handlebars version 1.3.0 in production, and I'd like to know what, if anything, we'll need to change in order to be able to upgrade to version 2.x. Since the HandlebarsJS team is committed to semantic versioning I know there must be some breaking changes, but I don't see them listed in the README.md. There are some items listed in the changelogs for the v2.0.0-alpha.N releases, but it's not clear to me whether or not this is a complete list (or if a complete list would even exist before 2.0.0 final is released). Some of the 1.x releases have "compatibilty notse" sections as well, but I believe they are all non-breaking/forward-compatible.

Could anyone offer some insight into the API differences or the goals/improvements of the 2.x series?

Compatiblity Notes

  • A JSON polyfill is required to run the compiler under IE8 and below. It's recommended that the precompiler be used in lieu of running the compiler on these legacy environments.
  • helperMissing helper no longer has the indexed name argument. Helper name is now available via options.name.
  • Precompiler output has changed, which breaks compatibility with prior versions of the runtime and precompiled output.
  • JavaScriptCompiler.compilerInfo now returns generic objects rather than javascript source.
  • AST changes
    • INTEGER -> NUMBER
    • Additional PartialNode hash parameter
    • New RawBlockNode type
  • Data frames now have a _parent field. This is internal but is enumerable for performance/compatability reasons.

Update: From the ember.js blog on 10/16/2014

In addition to the changes noted above:

Lines containing only block statements and whitespace are now removed. This matches the Mustache spec but may cause issues with code that expects whitespace to exist but would not otherwise.

like image 973
jacobq Avatar asked Jul 09 '14 19:07

jacobq


People also ask

What is the difference between HBS and HTML?

HTML is the language that browsers understand for laying out content on a web page. . hbs stands for Handlebars, the name of a tool that lets you write more than just HTML. When you start an app with ember serve , your templates are compiled down to something that Ember's rendering engine can process more easily.

What are Handlebars coding?

What is Handlebars? Handlebars is a simple templating language. It uses a template and an input object to generate HTML or other text formats. Handlebars templates look like regular text with embedded Handlebars expressions.

Should I use EJS or Handlebars?

EJS Is Way Faster Than Jade And Handlebars. EJS Has A Really Smart Error Handling Mechanism Built Right Into It. It Points Out To You, The Line Numbers On Which An Error Has Occurred So That You Don't End Up Looking Through The Whole Template File Wasting Your Time In Searching For Bugs.


1 Answers

If you look at changes between v1.3.0 and v2.0.0-alpha.1 releases of Handlebars accordingly to official Release Notes you will see, that was two major changes, that would break your templates during upgrade.

  1. Precompiler output has changed, which means your 1.x precompiled templates would not be compatible with Handlebars 2.x runtime, as well as 2.x runtime will be not compatible with old templates. You should update precompiler, update runtime and update all templates.
  2. Partials no longer have access to parent context (../), but now can accept hash as argument and has access to the root context through @root variable. So look at your partials for using ../ and change it to using local data, passed to partial as argument.

So, that's the main things you should pay attention when upgrading to 2.x Handlebars version. There was some internal changes which affect helperMissing helper, JavaScriptCompiler.compilerInfo, updates AST and data frames. But all of that makes sense only for users, who uses their own forks or making some of the modifications in runtime. Other changes was primarily bugfixes.

like image 124
raidendev Avatar answered Oct 20 '22 22:10

raidendev