• Stars
    star
    1,089
  • Rank 40,886 (Top 0.9 %)
  • Language
    Rust
  • License
    MIT License
  • Created over 9 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Rust templating with Handlebars

handlebars-rust

Handlebars templating language implemented in Rust and for Rust.

CI Coverage Status MIT licensed Docs Donate

Getting Started

Quick Start

use handlebars::Handlebars;
use serde_json::json;
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
    let mut reg = Handlebars::new();
    // render without register
    println!(
        "{}",
        reg.render_template("Hello {{name}}", &json!({"name": "foo"}))?
    );

    // register template using given name
    reg.register_template_string("tpl_1", "Good afternoon, {{name}}")?;
    println!("{}", reg.render("tpl_1", &json!({"name": "foo"}))?);

    Ok(())
}

Code Example

If you are not familiar with handlebars language syntax, it is recommended to walk through their introduction first.

Examples are provided in source tree to demo usage of various api.

  • quick the very basic example of registry and render apis
  • render how to define custom helpers with function, trait impl or macro, and also how to use custom helpers.
  • render_file similar to render, but render to file instead of string
  • helper_macro demos usage of handlebars_helper! to simplify helper development
  • partials template inheritance with handlebars
  • decorator how to use decorator to change data or define custom helper
  • script how to define custom helper with rhai scripting language, just like using javascript for handlebarsjs
  • error simple case for error
  • dev_mode a web server hosts handlebars in dev_mode, you can edit the template and see the change without restarting your server.

Web Playground

We have github action to compile latest master branch into WebAssembly and serve it on github pages. You can test and verify your template with both handlebars-rust and handlebarjs.

Minimum Rust Version Policy

Handlebars will track Rust nightly and stable channel. When dropping support for previous stable versions, I will bump patch version and clarify in CHANGELOG.

Docs

Rust doc.

Changelog

Changelog is available in the source tree named as CHANGELOG.md.

Contributor Guide

Any contribution to this library is welcomed. To get started into development, I have several Help Wanted issues, with the difficulty level labeled. When running into any problem, feel free to contact me on github.

I'm always looking for maintainers to work together on this library, let me know (via email or anywhere in the issue tracker) if you want to join.

Why (this) Handlebars?

Handlebars is a real-world templating system that you can use to build your application without pain.

Features

Isolation of Rust and HTML

This library doesn't attempt to use some macro magic to allow you to write your template within your rust code. I admit that it's fun to do that but it doesn't fit real-world use cases.

Limited but essential control structures built-in

Only essential control directives if and each are built-in. This prevents you from putting too much application logic into your template.

Extensible helper system

You can write your own helper with Rust! It can be a block helper or inline helper. Put your logic into the helper and don't repeat yourself.

A helper can be as a simple as a Rust function like:

handlebars_helper!(hex: |v: i64| format!("0x{:x}", v));

/// register the helper
handlebars.register_helper("hex", Box::new(hex));

And using it in your template:

{{hex 16}}

By default, handlebars-rust ships additional helpers (compared with original js version) that is useful when working with if.

With script_helper feature flag enabled, you can also create helpers using rhai script, just like JavaScript for handlebars-js. This feature was in early stage. Its API was limited at the moment, and can change in future.

Template inheritance

Every time I look into a templating system, I will investigate its support for template inheritance.

Template include is not sufficient for template reuse. In most cases you will need a skeleton of page as parent (header, footer, etc.), and embed your page into this parent.

You can find a real example of template inheritance in examples/partials.rs and templates used by this file.

Auto-reload in dev mode

By turning on dev_mode, handlebars auto reloads any template and scripts that loaded from files or directory. This can be handy for template development.

WebAssembly compatible

Handlebars 3.0 can be used in WebAssembly projects.

Fully scriptable

With rhai script support, you can implement your own helper with the scripting language. Together with the template lanaguage itself, template development can be fully scriptable without changing rust code.

Related Projects

Web frameworks

Adopters

The adopters page lists projects that uses handlebars for part of their functionalities.

Extensions

The extensions page has libraries that provide additional helpers, decorators and outputs to handlebars-rust, and you can use in your own projects.

License

This library (handlebars-rust) is open sourced under the MIT License.

More Repositories

1

node-geohash

geohash library for nodejs
JavaScript
555
star
2

heatcanvas

Pixel based heatmap with html5 canvas.
JavaScript
397
star
3

slacker

Transparent, non-incursive RPC by clojure and for clojure
Clojure
353
star
4

diehard

Clojure resilience library for flexible retry, circuit breaker and rate limiter
Clojure
309
star
5

ring-jetty9-adapter

An enhanced version of jetty adapter for ring, with additional features like websockets, http/2 and http/3
Clojure
228
star
6

pgwire

PostgreSQL wire protocol implemented as a rust library.
Rust
163
star
7

shake

clojure library that shakes your shell
Java
122
star
8

handlebars-iron

Handlebars middleware for Iron web framework
Rust
118
star
9

slacker-cluster

Clojure Micro-Service framework based on Slacker RPC
Clojure
77
star
10

pyclj

talking clojure with python - an edn implementation for python
Python
63
star
11

lein-jlink

A lein plugin creates and manages custom java environment
Clojure
59
star
12

hbs

clojure templating by handlebars.java
Clojure
48
star
13

papaline

Clojure concurrent pipleline on core.async
Clojure
47
star
14

rigui

Hierarchical Timing Wheels for Clojure and ClojureScript
Clojure
45
star
15

urdict

command line urban dictionary
Rust
42
star
16

stavka

Stavka manages configuration from various sources, for your Clojure application.
Clojure
41
star
17

exaile-doubanfm-plugin

An alternative choice to enjoy personalized music from douban.fm
Python
40
star
18

openbirdingmap

eBird location and observation data rendered on OpenStreetMap
Clojure
35
star
19

metriki

Rust metrics ported from dropwizard metrics
Rust
26
star
20

reddit.clj

A clojure wrapper for Reddit API
Clojure
25
star
21

rustmann

A riemann client on tokio, async/await
Rust
20
star
22

clojalk

A beanstalkd (distributed task queue) clone in clojure
Clojure
20
star
23

Exaile-Soundmenu-Indicator

integrate exaile with sound menu indicator
Python
19
star
24

cljts

A clojure wrapper of JTS, implements Simple Feature Specification of OGC
Clojure
15
star
25

clojuredocs-android

An Android app for ClojureDocs
JavaScript
14
star
26

lein-control

A lein plugin for clojure control
Clojure
12
star
27

tojson_macros

Simple rust syntax extension generates default ToJson implementation
Rust
11
star
28

timing

clojure syntax sugars for perf4j
Clojure
11
star
29

iron-json-response

Json response middleware for iron
Rust
10
star
30

stages

SEDA framework
Java
7
star
31

exaile-doubanfm-gnome-shell-extension

Exaile DoubanFM Control on GNOME-shell
JavaScript
7
star
32

bason

annotaion driven bson mapping tookit
Java
6
star
33

rageviewer

A rage comic viewer in clojure
Clojure
6
star
34

greptimedb-bin.aur

AUR repository for GreptimeDB
Shell
4
star
35

slacker-rust

Slacker RPC implemented by Rust and for Rust
Rust
4
star
36

lein-bootclasspath-deps

A Leiningen plugin to manage your bootclasspath.
Clojure
4
star
37

slacker-python

python client for slacker
Python
3
star
38

lazypress

Simplified online writing and publishing
JavaScript
3
star
39

sway-im.aur

Sway with input method popup support, packaged as AUR
Shell
2
star
40

mapzei

A maps extension for Ruman Nurik's live wallpaper app Muzei.
Java
2
star
41

karmalet

working in progress
Clojure
2
star
42

debug

Clojure port of nodejs debug
Clojure
2
star
43

link-socketio

Socketio server with link API
Clojure
2
star
44

nordenize

A project aimed to transform any webpage into nord color theme.
TypeScript
2
star
45

slacker-htrace

Distributed tracing support for Slacker RPC using Apache HTrace
Clojure
2
star
46

Artificial301

a simple firefox plugin and google appengine help you to get real link behind feedburner/feedproxy.google.com and url shorten services
JavaScript
2
star
47

athtool

Command line TOTP generator
Rust
1
star
48

alpine-jlink-base

Minimal base image for custom JRE created from jlink
1
star
49

handlebars-gotham

Handlebars templating middleware for Gotham
Rust
1
star
50

slacker-metrics

The metrics middleware for slacker RPC
Clojure
1
star
51

radar

redis proxy [WIP]
Clojure
1
star
52

metaq

A set of metaq API in favour of clojure
Clojure
1
star
53

leancloud-rtm-blacklist

LeanCloud 实时通信聊天室黑名单范例
JavaScript
1
star
54

datafusion-postgres

Rust
1
star