Hogan.js

raw JSON →
3.0.2 verified Fri May 01 auth: no javascript maintenance

Hogan.js is a Mustache templating compiler developed by Twitter, currently at version 3.0.2. It compiles Mustache templates into reusable HoganTemplate objects with a fast, three-phase process (scanning, parsing, code generation). It supports custom delimiters, template inheritance, pre-compilation, and a parser API for server-side processing. Key differentiators: standalone templates (render without the compiler), lambda features (optional), and compatibility with other Mustache implementations. Release cadence is irregular; latest release is from 2014.

error Hogan is not a constructor
cause Using `new Hogan(...)` instead of `Hogan.compile(...)`. Hogan is an object with static methods, not a constructor.
fix
Replace new Hogan(template) with Hogan.compile(template).
error Cannot find module 'hogan.js'
cause Missing npm install or incorrect import path.
fix
Run npm install hogan.js and ensure import/require path is correct (e.g., require('hogan.js')).
error HoganTemplate is not defined
cause Trying to use HoganTemplate class without importing it; it is a named export.
fix
Import HoganTemplate from 'hogan.js' or access it as Hogan.HoganTemplate.
gotcha Lambda features require the compiler and original template source; pre-compiled templates without the compiler cannot use lambdas.
fix Ensure the compiler and source are available at render time if using lambdas.
deprecated The project is no longer actively maintained; last release was in 2014. Security updates are not guaranteed.
fix Consider migrating to maintained alternatives like Handlebars or mustache.js.
gotcha Hogan.compile() with { asString: true } returns a string, not a template object; you must later instantiate a HoganTemplate.
fix Use `new HoganTemplate(compiledString)` after receiving the string.
gotcha Custom delimiters must be specified as a string with two delimiters separated by space, e.g., '<% %>'. Incorrect format will be silently ignored.
fix Check delimiter format; use exactly two tokens separated by a space.
npm install hogan.js
yarn add hogan.js
pnpm add hogan.js

Demonstrates basic template compilation and rendering using Hogan.js.

// Quickstart: compile and render a template
import Hogan from 'hogan.js';

const data = {
  name: 'World'
};

// Compile template string
const template = Hogan.compile('Hello {{name}}!');

// Render with data
const output = template.render(data);

console.log(output); // 'Hello World!'

// Pre-compile and render without compiler
const precompiled = Hogan.compile('Hello {{name}}!', { asString: true });
// Send precompiled string to client, then:
// const HoganTemplate = ... // need Hogan runtime
// const template = new HoganTemplate(precompiled);
// template.render(data);