Rocket - Hello World with Tera template



examples/rocket/hello-world-tera-template/Cargo.toml
[package]
name = "hello-world-tera-template"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rocket = "0.5"
rocket_dyn_templates = { version = "0.1", features = ["tera"] }

examples/rocket/hello-world-tera-template/src/main.rs
#[macro_use]
extern crate rocket;

use rocket_dyn_templates::{context, Template};

#[get("/")]
fn index() -> Template {
    Template::render(
        "index",
        context! {
            name: "Rocket with Tera"
        },
    )
}

#[launch]
fn rocket() -> _ {
    rocket::build()
        .mount("/", routes![index])
        .attach(Template::fairing())
}

#[cfg(test)]
mod tests;

examples/rocket/hello-world-tera-template/src/tests.rs
use rocket::http::Status;
use rocket::local::blocking::Client;

#[test]
fn hello_world() {
    let client = Client::tracked(super::rocket()).unwrap();
    let response = client.get("/").dispatch();

    assert_eq!(response.status(), Status::Ok);
    assert_eq!(
        response.headers().get_one("Content-Type").unwrap(),
        "text/html; charset=utf-8"
    );
    assert_eq!(
        response.into_string(),
        Some("Hello <b>Rocket with Tera!</b>".into())
    );
}

examples/rocket/hello-world-tera-template/templates/index.html.tera
Hello <b>{{ name }}!</b>