How to Use Templates in Rust (Tera, Askama)

Web
Use the askama crate to define Rust structs with the Template trait and render HTML files by calling the render method.

Use the askama crate to compile templates at build time and render them with Rust structs. Add askama and askama_derive to your dependencies, define a struct matching your template data, and implement the Template trait using the #[template] attribute. Then, call .render() on your struct instance to get the final string.

use askama::Template;

#[derive(Template)]
#[template(path = "hello.html")]
struct Greeting<'a> {
    name: &'a str,
}

fn main() {
    let greeting = Greeting { name: "World" };
    println!("{}", greeting.render().unwrap());
}

Create a hello.html file in your templates directory with content like Hello, {{ name }}!.