Top Related Projects
Logic-less Ruby templates.
Minimal templating on steroids.
Minimal templating with {{mustaches}} in JavaScript
Asynchronous Javascript templating for the browser and server
Pug – robust, elegant, feature rich template engine for Node.js
Quick Overview
Hogan.js is a JavaScript templating engine developed by Twitter. It implements the Mustache templating language in JavaScript, allowing for fast and efficient rendering of templates both on the server-side and client-side.
Pros
- Fast compilation and rendering of templates
- Compatible with both browser and Node.js environments
- Supports pre-compilation of templates for improved performance
- Lightweight and has no dependencies
Cons
- Limited feature set compared to some more modern templating engines
- Lack of recent updates or active maintenance
- Learning curve for those unfamiliar with Mustache syntax
- No built-in support for more advanced templating features like loops or conditionals
Code Examples
- Basic template rendering:
var template = Hogan.compile('Hello {{name}}!');
var output = template.render({ name: 'World' });
console.log(output); // Outputs: Hello World!
- Using sections for conditional rendering:
var template = Hogan.compile('{{#has_items}}You have items!{{/has_items}}{{^has_items}}No items.{{/has_items}}');
var output1 = template.render({ has_items: true });
var output2 = template.render({ has_items: false });
console.log(output1); // Outputs: You have items!
console.log(output2); // Outputs: No items.
- Iterating over an array:
var template = Hogan.compile('{{#items}}<li>{{.}}</li>{{/items}}');
var output = template.render({ items: ['apple', 'banana', 'orange'] });
console.log(output); // Outputs: <li>apple</li><li>banana</li><li>orange</li>
Getting Started
To use Hogan.js in your project, follow these steps:
-
Install Hogan.js via npm:
npm install hogan.js
-
In your JavaScript file, require Hogan:
var Hogan = require('hogan.js');
-
Compile and render a template:
var template = Hogan.compile('Hello {{name}}!'); var output = template.render({ name: 'World' }); console.log(output);
For browser usage, include the Hogan.js script in your HTML file and use it directly in your JavaScript code.
Competitor Comparisons
Logic-less Ruby templates.
Pros of Mustache
- More widely adopted and supported across multiple programming languages
- Simpler syntax and easier to learn for beginners
- Strict logic-less templating, promoting cleaner separation of concerns
Cons of Mustache
- Slower rendering performance compared to Hogan.js
- Lacks pre-compilation feature for improved runtime performance
- More limited functionality, missing some advanced features present in Hogan.js
Code Comparison
Mustache:
var view = {
name: "Joe",
greeting: function() {
return "Welcome!";
}
};
var output = Mustache.render("{{greeting}} {{name}}", view);
Hogan.js:
var template = Hogan.compile("{{greeting}} {{name}}");
var view = {
name: "Joe",
greeting: function() {
return "Welcome!";
}
};
var output = template.render(view);
Both Mustache and Hogan.js are templating engines, with Hogan.js being Twitter's implementation of Mustache. While they share similar syntax, Hogan.js offers performance optimizations and additional features. Mustache focuses on simplicity and language-agnostic implementation, making it a popular choice for various projects. Hogan.js, on the other hand, provides better performance through pre-compilation and is well-suited for high-performance applications, particularly those built with Twitter's tech stack.
Minimal templating on steroids.
Pros of Handlebars.js
- More feature-rich with built-in helpers and custom helper support
- Easier to read syntax with cleaner templates
- Supports precompilation for better performance
Cons of Handlebars.js
- Larger file size and potentially slower runtime performance
- Steeper learning curve due to more complex syntax and features
- Less suitable for simple templating needs
Code Comparison
Handlebars.js:
{{#each items}}
<li>{{name}}: {{price}}</li>
{{/each}}
Hogan.js:
{{#items}}
<li>{{name}}: {{price}}</li>
{{/items}}
Both Handlebars.js and Hogan.js are popular templating engines for JavaScript. Handlebars.js offers more features and a cleaner syntax, making it suitable for complex templating needs. It provides built-in helpers and allows custom helpers, enhancing template flexibility.
Hogan.js, developed by Twitter, is lighter and faster, making it ideal for simpler templating requirements. It has a smaller footprint and potentially better runtime performance, which can be advantageous for projects prioritizing speed and minimalism.
The code comparison shows that both engines use similar syntax for basic templating tasks. However, Handlebars.js offers more advanced features and cleaner syntax for complex scenarios, while Hogan.js maintains simplicity and performance for straightforward templating needs.
Choosing between the two depends on project requirements, complexity, and performance considerations.
Minimal templating with {{mustaches}} in JavaScript
Pros of Mustache.js
- More widely adopted and has a larger community
- Supports a broader range of programming languages
- Simpler syntax and easier to learn for beginners
Cons of Mustache.js
- Generally slower performance compared to Hogan.js
- Lacks pre-compilation feature, which can impact runtime performance
- Less optimized for large-scale applications
Code Comparison
Mustache.js:
var view = {
name: "Joe",
greeting: function() {
return "Welcome " + this.name;
}
};
Mustache.render("{{greeting}}", view);
Hogan.js:
var template = Hogan.compile("{{greeting}}");
var view = {
name: "Joe",
greeting: function() {
return "Welcome " + this.name;
}
};
template.render(view);
Both Mustache.js and Hogan.js are popular templating engines for JavaScript. Mustache.js offers broader language support and a simpler syntax, making it more accessible for beginners. However, Hogan.js, developed by Twitter, provides better performance and pre-compilation features, making it more suitable for large-scale applications.
The code comparison shows that Hogan.js requires an additional compilation step, which can improve runtime performance. Mustache.js, on the other hand, offers a more straightforward rendering process.
Ultimately, the choice between these two libraries depends on the specific needs of your project, considering factors such as performance requirements, ease of use, and scalability.
Asynchronous Javascript templating for the browser and server
Pros of Dustjs
- More feature-rich templating language with advanced control structures
- Supports asynchronous template loading and rendering
- Extensive documentation and community support
Cons of Dustjs
- Steeper learning curve due to more complex syntax
- Slightly slower rendering performance compared to Hogan.js
- Larger file size and potentially increased load times
Code Comparison
Dustjs:
{#users}
<h3>{name}</h3>
<p>{email}</p>
{#address}
<address>{street}, {city}, {state} {zip}</address>
{/address}
{/users}
Hogan.js:
{{#users}}
<h3>{{name}}</h3>
<p>{{email}}</p>
{{#address}}
<address>{{street}}, {{city}}, {{state}} {{zip}}</address>
{{/address}}
{{/users}}
Both templating engines use similar mustache-like syntax, but Dustjs offers more advanced features and control structures. Hogan.js focuses on simplicity and performance, while Dustjs provides a more comprehensive templating solution with additional functionality.
Dustjs is better suited for complex templating needs and larger applications, while Hogan.js excels in simpler use cases where performance is a priority. The choice between the two depends on the specific requirements of your project and the level of complexity needed in your templates.
Pug – robust, elegant, feature rich template engine for Node.js
Pros of Pug
- More feature-rich templating language with advanced functionality like mixins, includes, and inheritance
- Cleaner, more concise syntax with significant whitespace and minimal punctuation
- Supports inline JavaScript for dynamic content generation
Cons of Pug
- Steeper learning curve due to its unique syntax and advanced features
- Less performant for large-scale applications compared to Hogan.js
- Not as widely adopted in the industry as some other templating engines
Code Comparison
Pug:
doctype html
html(lang="en")
head
title= pageTitle
body
h1 Welcome to #{siteName}
Hogan.js:
<!DOCTYPE html>
<html lang="en">
<head><title>{{pageTitle}}</title></head>
<body><h1>Welcome to {{siteName}}</h1></body>
</html>
Summary
Pug offers a more powerful and expressive templating language with a cleaner syntax, making it suitable for complex projects. However, it comes with a steeper learning curve and potential performance trade-offs. Hogan.js, on the other hand, provides a simpler, logic-less templating solution that's faster and easier to learn but less feature-rich. The choice between the two depends on project requirements, team expertise, and performance considerations.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Hogan.js - A mustache compiler.
Hogan.js is a compiler for the Mustache templating language. For information on Mustache, see the manpage and the spec.
Basics
Hogan compiles templates to HoganTemplate objects, which have a render method.
var data = {
screenName: "dhg",
};
var template = Hogan.compile("Follow @{{screenName}}.");
var output = template.render(data);
// prints "Follow @dhg."
console.log(output);
Features
Hogan is fast--try it on your workload.
Hogan has separate scanning, parsing and code generation phases. This way it's possible to add new features without touching the scanner at all, and many different code generation techniques can be tried without changing the parser.
Hogan exposes scan and parse methods. These can be useful for pre-processing templates on the server.
var text = "{{^check}}{{#i18n}}No{{/i18n}}{{/check}}";
text += "{{#check}}{{#i18n}}Yes{{/i18n}}{{/check}}";
var tree = Hogan.parse(Hogan.scan(text));
// outputs "# check"
console.log(tree[0].tag + " " + tree[0].name);
// outputs "Yes"
console.log(tree[1].nodes[0].nodes[0]);
It's also possible to use HoganTemplate objects without the Hogan compiler present. That means you can pre-compile your templates on the server, and avoid shipping the compiler. However, the optional lambda features from the Mustache spec require the compiler and the original template source to be present.
Hogan also supports template inheritance, and maintains compatibility with other implementations like mustache.java, mustache.php, and GRMustache
Why Hogan.js?
Why another templating library?
Hogan.js was written to meet three templating library requirements: good performance, standalone template objects, and a parser API.
Install
Node.js
npm install hogan.js
component
component install twitter/hogan.js
Compilation options
The second argument to Hogan.compile is an options hash.
var text = "my <%example%> template."
Hogan.compile(text, {delimiters: '<% %>'});
There are currently four valid options.
asString: return the compiled template as a string. This feature is used by hulk to produce strings containing pre-compiled templates.
sectionTags: allow custom tags that require opening and closing tags, and treat them as though they were section tags.
var text = "my {{_foo}}example{{/foo}} template."
Hogan.compile(text, { sectionTags: [{o: '_foo', c: 'foo'}]});
The value is an array of object with o and c fields that indicate names for custom section tags. The example above allows parsing of {{_foo}}{{/foo}}.
delimiters: A string that overrides the default delimiters. Example: "<% %>".
disableLambda: disables the higher-order sections / lambda-replace features of Mustache.
Issues
Have a bug? Please create an issue here on GitHub!
https://github.com/twitter/hogan.js/issues
Versioning
For transparency and insight into our release cycle, releases will be numbered with the follow format:
<major>.<minor>.<patch>
And constructed with the following guidelines:
- Breaking backwards compatibility bumps the major
- New additions without breaking backwards compatibility bumps the minor
- Bug fixes and misc changes bump the patch
For more information on semantic versioning, please visit http://semver.org/.
Testing
To run the tests you first need to update all git submodules.
$ git submodule init
$ git submodule update
Unit tests are written using QUnit. To run them, open test/index.html
in a browser.
Use node to run all tests from the mustache spec.
$ node test/spec.js
Authors
Robert Sayre
Jacob Thornton
License
Copyright 2011 Twitter, Inc.
Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0
Top Related Projects
Logic-less Ruby templates.
Minimal templating on steroids.
Minimal templating with {{mustaches}} in JavaScript
Asynchronous Javascript templating for the browser and server
Pug – robust, elegant, feature rich template engine for Node.js
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot