Why handlebars.js




















Helper calls may also have literal values passed to them either as parameter arguments or hash arguments. Supported literals include numbers, strings, true , false , null and undefined :. Handlebars provides additional metadata, such as Hash arguments, to helpers as a final parameter. The keys in hash arguments must each be simple identifiers, and the values are Handlebars expressions.

This means that values can be simple identifiers, paths, or Strings. If we pass the below input to the template, the value of person. As described in the helper script below, the hash arguments can be obtained from the last parameter options for further processing within the helper. Handlebars also offers a mechanism for invoking a helper with a block of the template.

Block helpers can then invoke that block zero or more times with any context it chooses. If a helper is registered by the same name as a property of an input object, the helper has priority over the input property. If you want to resolve the input property instead, you can prefix its name with.

The comments will not be in the resulting output. If you'd like the comments to show up. Just use html comments, and they will be output. Handlebars helpers can be accessed from any context in a template. You can register a helper with the Handlebars. Block expressions allow you to define helpers that will invoke a section of your template with a different context than the current. Let's consider a helper that will generate an HTML list:. The example creates a helper named list to generate our HTML list.

The helper receives the people as its first parameter, and an options hash as its second parameter. Here is a short list:. Templates can be written both in the HTML file or separately. Here is an example:. With this markup in place, we can see what we have to do use it. After the template has been retrieved, we can compile it by using the Handlebars.

This function is then executed by passing the context as an argument. When the execution is complete, the function returns the desired HTML with all the variables replaced by their corresponding values.

At this point we can inject the HTML into our web page. A live demo of this code can be found in this Codepen demo.

We already saw expressions in the above section. Handlebars can escape the value returned by the expression. For example, when the following template:. A live demo that shows this feature can be found in this Codepen demo. We can also write comments inside Handlebars templates. A demo of this example can be found here. For now, take a look at how an if block is written:.

Handlebars supports both normal and nested path, making it possible to look up properties nested below the current context. In a rough performance test, precompiled Handlebars. It would be a shame if it were any other way, since they were precompiled, but the difference in architecture does have some big performance advantages. Justin Marney, a. The rewritten Handlebars current version is faster than the old version, with many performance tests being 5 to 7 times faster than the Mustache equivalent.

See release-notes. See FAQ. Have a project using Handlebars? Send us a pull request! Git github.



0コメント

  • 1000 / 1000