Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork152
Rust templating with Handlebars
License
sunng87/handlebars-rust
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Handlebars templating language implementedin Rust and for Rust.
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(())}
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 of
handlebars_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 in
dev_mode
, you can edit the template and see the changewithout restarting your server.
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.
Handlebars will track Rust nightly and stable channel. When droppingsupport for previous stable versions, I will bumppatch versionand clarify in CHANGELOG.
Changelog is available in the source tree named asCHANGELOG.md
.
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.
Handlebars is a real-world templating system that you can use to buildyour application without pain.
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.
Only essential control directivesif
andeach
are built-in. Thisprevents you from putting too much application logic into your template.
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.
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.
By turning ondev_mode
, handlebars auto reloads any template and scripts thatloaded from files or directory. This can be handy for template development.
Handlebars 3.0 can be used in WebAssembly projects.
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.
- Iron:handlebars-iron
- Rocket:rocket/contrib
- Warp:handlebarsexample
- Tower-web:Built-in
- Actix:handlebarsexample
- Tide:tide-handlebars
- Axum:axum-template
Theadopterspage lists projects that uses handlebars for part of theirfunctionalities.
Theextensionspage has libraries that provide additional helpers, decorators andoutputs to handlebars-rust, and you can use in your own projects.
This library (handlebars-rust) is open sourced under the MIT License.
About
Rust templating with Handlebars
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.