Handlebars - render to file



examples/handlebars/handlebars-render-to-file/src/main.rs
use handlebars::Handlebars;
use serde_json::json;
use std::error::Error;
use std::fs::File;
use std::io::Write;

fn main() {
    match render_without_register("hello.html") {
        Ok(_) => println!(),
        Err(_) => println!("error"),
    }
}

fn render_without_register(filename: &str) -> Result<(), Box<dyn Error>> {
    let reg = Handlebars::new();
    let html = reg.render_template("Hello {{name}}", &json!({"name": "foo"}))?;
    let mut file = File::create(filename).unwrap();
    writeln!(&mut file, "{}", html).unwrap();

    Ok(())
}

examples/handlebars/handlebars-render-to-file/hello.html
Hello foo

examples/handlebars/handlebars-render-to-file/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"