Skip to content

Installing a binding ​

Herb is a C library with bindings for Ruby, JavaScript, Java and Rust. Every binding wraps the same parser, so a template parses to the same tree whichever one you use.

Install ​

shell
bundle add herb
shell
npm add @herb-tools/node
shell
git clone https://github.com/marcoroth/herb
cd herb/java
make templates
make jni
make java
shell
cargo add herb

The Ruby gem and the npm packages ship precompiled. Java builds from source and produces a native library, libherb_jni.dylib on macOS and libherb_jni.so on Linux.

Load ​

ruby
require "herb"

Herb.parse("<h1><%= title %></h1>")
js
import { Herb } from "@herb-tools/node"

await Herb.load()

Herb.parse("<h1><%= title %></h1>")
java
import org.herb.Herb;
import org.herb.ParseResult;

ParseResult result = Herb.parse("<h1><%= title %></h1>");
rust
use herb::parse;

let result = parse("<h1><%= title %></h1>").unwrap();

JavaScript is the one binding that has to be loaded before use. Herb.load() resolves once the native extension or the WebAssembly build is ready, and every call after that is synchronous.

Which package ​

LanguagePackageNotes
RubyherbPrecompiled native extension
JavaScript, Node.js@herb-tools/nodeNative extension, reads files
JavaScript, browser@herb-tools/browserWebAssembly, no file access
Javabuilt from sourceJNI, needs the native library on java.library.path
RustherbFFI to the C library

The browser and Node packages expose the same API, so code that does not touch the filesystem moves between them unchanged. WebAssembly covers the browser build, and the per-language pages cover the rest of each setup, including unreleased commits from npm and the Java classpath.

Next ​

Parsing is where most work starts. Lexing gives you tokens instead of a tree, Extracting Ruby and HTML pulls one language out of a template, and Working with the tree walks what the parser returned.

Released under the MIT License.