Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Rust templating with Handlebars

License

NotificationsYou must be signed in to change notification settings

sunng87/handlebars-rust

Repository files navigation

Handlebars templating language implementedin Rust and for Rust.

CICoverage StatusMIT licensedDocsDonate

Getting Started

Quick Start

use handlebars::Handlebars;use serde_json::json;use std::error::Error;fnmain() ->Result<(),Box<dynError>>{letmut reg =Handlebars::new();// render without registerprintln!("{}",        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 withhandlebars languagesyntax, it is recommended to walk throughtheir introduction first.

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

  • quickthe very basic example of registry and render apis
  • renderhow to define custom helpers with function, trait impl or macro, and also howto use custom helpers.
  • render_filesimilar to render, but render to file instead of string
  • helper_macrodemos usage ofhandlebars_helper! to simplify helper development
  • partialstemplate inheritance with handlebars
  • decoratorhow to use decorator to change data or define custom helper
  • scripthow to define custom helper with rhai scripting language,just like using javascript for handlebarsjs
  • errorsimple case for error
  • dev_modea web server hosts handlebars indev_mode, you can edit the template and see the changewithout restarting your server.

Web Playground

We have github action to compile latestmaster branch into WebAssembly andserve it ongithub pages. You cantest and verify your template with both handlebars-rust and handlebarjs.

Minimum Rust Version Policy

Handlebars will track Rust nightly and stable channel. When droppingsupport for previous stable versions, I will bumppatch versionand clarify in CHANGELOG.

Docs

Rust doc.

Changelog

Changelog is available in the source tree named asCHANGELOG.md.

Contributor Guide

Any contribution to this library is welcomed. To get started intodevelopment, I have severalHelpWantedissues, 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 youwant to join.

Why (this) Handlebars?

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

Features

Isolation of Rust and HTML

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

Limited but essential control structures built-in

Only essential control directivesif andeach are built-in. Thisprevents 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 orinline helper. Put your logic into the helper and don't repeatyourself.

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

handlebars_helper!(hex: |v:i64| format!("0x{:x}", v));/// register the helperhandlebars.register_helper("hex",Box::new(hex));

And using it in your template:

{{hex16}}

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

Withscript_helper feature flag enabled, you can also create helpersusingrhai script, just like JavaScriptfor handlebars-js. This feature was in early stage. Its API was limited at themoment, and can change in future.

Template inheritance

Every time I look into a templating system, I will investigate itssupport fortemplateinheritance.

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

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

Auto-reload in dev mode

By turning ondev_mode, handlebars auto reloads any template and scripts thatloaded from files or directory. This can be handy for template development.

WebAssembly compatible

Handlebars 3.0 can be used in WebAssembly projects.

Fully scriptable

Withrhai script support, youcan implement your own helper with the scripting language. Togetherwith the template lanaguage itself, template development can be fullyscriptable without changing rust code.

Related Projects

Web frameworks

Adopters

Theadopterspage lists projects that uses handlebars for part of theirfunctionalities.

Extensions

Theextensionspage has libraries that provide additional helpers, decorators andoutputs to handlebars-rust, and you can use in your own projects.

License

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

About

Rust templating with Handlebars

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

  •  

Contributors82

Languages


[8]ページ先頭

©2009-2025 Movatter.jp