- Handlebars
- render_template
- json
Quick start - unregistered template
- Handlebars is templating language implemented in Rust. It is based on the ideas of the original HandlebarsJS but it is not 100% compatible.
- We have a very simple template embedded in our Rust code. It has one placeholder {{name}}.
- We pass a JSON string to the render_template function.
examples/handlebars/handlebars-quick/src/main.rs
use handlebars::Handlebars; use serde_json::json; use std::error::Error; fn main() { match render() { Ok(_) => println!(), Err(_) => println!("error"), } } fn render() -> Result<(), Box<dyn Error>> { let reg = Handlebars::new(); println!( "{}", reg.render_template("Hello {{name}}", &json!({"name": "foo"}))? ); Ok(()) }
- The generated output looks like this:
examples/handlebars/handlebars-quick/out.html
Hello foo
examples/handlebars/handlebars-quick/Cargo.toml
[package] name = "handlebars-quick" version = "0.1.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] handlebars = "4.3.7" serde_json = "1.0.97"