From 6bc400104eee68d677c004996ae2030d39977d9f Mon Sep 17 00:00:00 2001 From: Deffendor <21102647+Deffendor@users.noreply.github.com> Date: Sun, 11 Feb 2024 12:17:41 +0100 Subject: [PATCH] add minijinja templating example (#2523) --- examples/templates-minijinja/Cargo.toml | 10 +++ examples/templates-minijinja/src/main.rs | 87 +++++++++++++++++++ .../templates-minijinja/templates/about.jinja | 6 ++ .../templates/content.jinja | 10 +++ .../templates-minijinja/templates/home.jinja | 6 ++ .../templates/layout.jinja | 14 +++ 6 files changed, 133 insertions(+) create mode 100644 examples/templates-minijinja/Cargo.toml create mode 100644 examples/templates-minijinja/src/main.rs create mode 100644 examples/templates-minijinja/templates/about.jinja create mode 100644 examples/templates-minijinja/templates/content.jinja create mode 100644 examples/templates-minijinja/templates/home.jinja create mode 100644 examples/templates-minijinja/templates/layout.jinja diff --git a/examples/templates-minijinja/Cargo.toml b/examples/templates-minijinja/Cargo.toml new file mode 100644 index 00000000..9b525a8c --- /dev/null +++ b/examples/templates-minijinja/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "example-templates-minijinja" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] +axum = { path = "../../axum" } +minijinja = "1.0.11" +tokio = { version = "1.0", features = ["full"] } diff --git a/examples/templates-minijinja/src/main.rs b/examples/templates-minijinja/src/main.rs new file mode 100644 index 00000000..2903b824 --- /dev/null +++ b/examples/templates-minijinja/src/main.rs @@ -0,0 +1,87 @@ +//! Run with +//! +//! ```not_rust +//! cargo run -p example-templates-minijinja +//! ``` +//! Demo for the MiniJinja templating engine. +//! Exposes three pages all sharing the same layout with a minimal nav menu. + +use axum::extract::State; +use axum::http::StatusCode; +use axum::{response::Html, routing::get, Router}; +use minijinja::{context, Environment}; +use std::sync::Arc; + +struct AppState { + env: Environment<'static>, +} + +#[tokio::main] +async fn main() { + // init template engine and add templates + let mut env = Environment::new(); + env.add_template("layout", include_str!("../templates/layout.jinja")) + .unwrap(); + env.add_template("home", include_str!("../templates/home.jinja")) + .unwrap(); + env.add_template("content", include_str!("../templates/content.jinja")) + .unwrap(); + env.add_template("about", include_str!("../templates/about.jinja")) + .unwrap(); + + // pass env to handlers via state + let app_state = Arc::new(AppState { env }); + + // define routes + let app = Router::new() + .route("/", get(handler_home)) + .route("/content", get(handler_content)) + .route("/about", get(handler_about)) + .with_state(app_state); + + // run it + let listener = tokio::net::TcpListener::bind("127.0.0.1:3000") + .await + .unwrap(); + println!("listening on {}", listener.local_addr().unwrap()); + axum::serve(listener, app).await.unwrap(); +} + +async fn handler_home(State(state): State>) -> Result, StatusCode> { + let template = state.env.get_template("home").unwrap(); + + let rendered = template + .render(context! { + title => "Home", + welcome_text => "Hello World!", + }) + .unwrap(); + + Ok(Html(rendered)) +} + +async fn handler_content(State(state): State>) -> Result, StatusCode> { + let template = state.env.get_template("content").unwrap(); + + let some_example_entries = vec!["Data 1", "Data 2", "Data 3"]; + + let rendered = template + .render(context! { + title => "Content", + entries => some_example_entries, + }) + .unwrap(); + + Ok(Html(rendered)) +} + +async fn handler_about(State(state): State>) -> Result, StatusCode> { + let template = state.env.get_template("about").unwrap(); + + let rendered = template.render(context!{ + title => "About", + about_text => "Simple demonstration layout for an axum project with minijinja as templating engine.", + }).unwrap(); + + Ok(Html(rendered)) +} diff --git a/examples/templates-minijinja/templates/about.jinja b/examples/templates-minijinja/templates/about.jinja new file mode 100644 index 00000000..ba8c97e3 --- /dev/null +++ b/examples/templates-minijinja/templates/about.jinja @@ -0,0 +1,6 @@ +{% extends "layout" %} +{% block title %}{{ super() }} | {{ title }} {% endblock %} +{% block body %} +

{{ title }}

+

{{ about_text }}

+{% endblock %} diff --git a/examples/templates-minijinja/templates/content.jinja b/examples/templates-minijinja/templates/content.jinja new file mode 100644 index 00000000..b3fbfa6c --- /dev/null +++ b/examples/templates-minijinja/templates/content.jinja @@ -0,0 +1,10 @@ +{% extends "layout" %} +{% block title %}{{ super() }} | {{ title }} {% endblock %} +{% block body %} +

{{ title }}

+{% for data_entry in entries %} +
    +
  • {{ data_entry }}
  • +
+{% endfor %} +{% endblock %} diff --git a/examples/templates-minijinja/templates/home.jinja b/examples/templates-minijinja/templates/home.jinja new file mode 100644 index 00000000..2d231db3 --- /dev/null +++ b/examples/templates-minijinja/templates/home.jinja @@ -0,0 +1,6 @@ +{% extends "layout" %} +{% block title %}{{ super() }} | {{ title }} {% endblock %} +{% block body %} +

{{ title }}

+

{{ welcome_text }}

+{% endblock %} diff --git a/examples/templates-minijinja/templates/layout.jinja b/examples/templates-minijinja/templates/layout.jinja new file mode 100644 index 00000000..9ef56205 --- /dev/null +++ b/examples/templates-minijinja/templates/layout.jinja @@ -0,0 +1,14 @@ + + + {% block title %}Website Name{% endblock %} + + + {% block body %}{% endblock %} + +