aboutsummaryrefslogtreecommitdiffstats
path: root/web/template
diff options
context:
space:
mode:
authormsi2025-11-13 14:07:03 -0300
committermsi2025-11-13 14:07:03 -0300
commit017324a1e98ac2da7812e4fcc06fe67d424e4ffb (patch)
tree85e1901cfdfd2b370c73f03796626c9c814b04e9 /web/template
parentb863a55d8a947cfcc6296d34edae4d1365a7725f (diff)
downloadtemplates-017324a1e98ac2da7812e4fcc06fe67d424e4ffb.tar.gz
Implement minijinja template
Diffstat (limited to 'web/template')
-rw-r--r--web/template/Cargo.toml1
-rw-r--r--web/template/src/main.rs41
-rw-r--r--web/template/src/router.rs86
-rw-r--r--web/template/src/state.rs21
-rw-r--r--web/template/templates/about.jinja6
-rw-r--r--web/template/templates/content.jinja10
-rw-r--r--web/template/templates/home.jinja6
-rw-r--r--web/template/templates/layout.jinja17
8 files changed, 160 insertions, 28 deletions
diff --git a/web/template/Cargo.toml b/web/template/Cargo.toml
index 6890378..0c41cb9 100644
--- a/web/template/Cargo.toml
+++ b/web/template/Cargo.toml
@@ -9,6 +9,7 @@ edition = "2024"
[dependencies]
anyhow = "=1.0.100"
axum = "=0.8.6"
+minijinja = "=2.12.0"
serde = { version = "=1.0.228", features = ["derive"] }
tokio = { version = "=1.48.0", features = ["macros", "rt-multi-thread", "signal"] }
tower-http = { version = "=0.6.6", features = ["timeout", "trace", "fs"] }
diff --git a/web/template/src/main.rs b/web/template/src/main.rs
index d686877..20a1974 100644
--- a/web/template/src/main.rs
+++ b/web/template/src/main.rs
@@ -14,29 +14,29 @@
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
-use std::time::Duration;
+use std::sync::Arc;
-use axum::{Router, response::Html, routing::get};
+use minijinja::Environment;
use tokio::net::TcpListener;
-use tower_http::{
- services::ServeDir, timeout::TimeoutLayer, trace::TraceLayer,
-};
use tracing::info;
mod helpers;
+mod router;
+mod state;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
helpers::init_tracing();
- let app = Router::new()
- .route("/", get(handler))
- // TODO(msi): from config folder asssets
- .nest_service("/assets", ServeDir::new("assets"))
- .layer((
- TraceLayer::new_for_http(),
- TimeoutLayer::new(Duration::from_secs(10)), // TODO(msi): from config
- ));
+ let mut env = Environment::new();
+ env.add_template("layout", include_str!("../templates/layout.jinja"))?;
+ env.add_template("home", include_str!("../templates/home.jinja"))?;
+ env.add_template("content", include_str!("../templates/content.jinja"))?;
+ env.add_template("about", include_str!("../templates/about.jinja"))?;
+
+ let app_state = Arc::new(state::AppState { env });
+
+ let app = router::route(app_state);
// TODO(msi): from config
let listener = TcpListener::bind("0.0.0.0:3000").await?;
@@ -47,18 +47,3 @@ async fn main() -> anyhow::Result<()> {
Ok(())
}
-
-const INDEX: &'static str = r#"<html>
-<head>
-<link href="/assets/css/styles.css" rel="stylesheet" type="text/css">
-</head>
-<body>
-<h1><h1>Hello, World {{project-name}} =]</h1>
-<p>Template form https://ijanc.org</p>
-</body>
-</html>
-"#;
-
-async fn handler() -> Html<&'static str> {
- Html(INDEX)
-}
diff --git a/web/template/src/router.rs b/web/template/src/router.rs
new file mode 100644
index 0000000..d1c8ba2
--- /dev/null
+++ b/web/template/src/router.rs
@@ -0,0 +1,86 @@
+//
+// Copyright (c) 2025 murilo ijanc' <murilo@ijanc.org>
+//
+// Permission to use, copy, modify, and distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+//
+use std::{sync::Arc, time::Duration};
+
+use axum::{
+ Router, extract::State, http::StatusCode, response::Html, routing::get,
+};
+use minijinja::context;
+use tower_http::{
+ services::ServeDir, timeout::TimeoutLayer, trace::TraceLayer,
+};
+
+use crate::state::AppState;
+
+pub(crate) fn route(app_state: Arc<AppState>) -> Router {
+ Router::new()
+ .route("/", get(handler_home))
+ .route("/content", get(handler_content))
+ .route("/about", get(handler_about))
+ // TODO(msi): from config folder asssets
+ .nest_service("/assets", ServeDir::new("assets"))
+ .layer((
+ TraceLayer::new_for_http(),
+ // TODO(msi): from config
+ TimeoutLayer::new(Duration::from_secs(10)),
+ ))
+ .with_state(app_state)
+}
+
+async fn handler_home(
+ State(state): State<Arc<AppState>>,
+) -> Result<Html<String>, 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<Arc<AppState>>,
+) -> Result<Html<String>, 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<Arc<AppState>>,
+) -> Result<Html<String>, 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/web/template/src/state.rs b/web/template/src/state.rs
new file mode 100644
index 0000000..d479383
--- /dev/null
+++ b/web/template/src/state.rs
@@ -0,0 +1,21 @@
+//
+// Copyright (c) 2025 murilo ijanc' <murilo@ijanc.org>
+//
+// Permission to use, copy, modify, and distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+//
+
+use minijinja::Environment;
+
+pub(crate) struct AppState {
+ pub(crate) env: Environment<'static>,
+}
diff --git a/web/template/templates/about.jinja b/web/template/templates/about.jinja
new file mode 100644
index 0000000..ba8c97e
--- /dev/null
+++ b/web/template/templates/about.jinja
@@ -0,0 +1,6 @@
+{% extends "layout" %}
+{% block title %}{{ super() }} | {{ title }} {% endblock %}
+{% block body %}
+<h1>{{ title }}</h1>
+<p>{{ about_text }}</p>
+{% endblock %}
diff --git a/web/template/templates/content.jinja b/web/template/templates/content.jinja
new file mode 100644
index 0000000..b3fbfa6
--- /dev/null
+++ b/web/template/templates/content.jinja
@@ -0,0 +1,10 @@
+{% extends "layout" %}
+{% block title %}{{ super() }} | {{ title }} {% endblock %}
+{% block body %}
+<h1>{{ title }}</h1>
+{% for data_entry in entries %}
+<ul>
+ <li>{{ data_entry }}</li>
+</ul>
+{% endfor %}
+{% endblock %}
diff --git a/web/template/templates/home.jinja b/web/template/templates/home.jinja
new file mode 100644
index 0000000..2d231db
--- /dev/null
+++ b/web/template/templates/home.jinja
@@ -0,0 +1,6 @@
+{% extends "layout" %}
+{% block title %}{{ super() }} | {{ title }} {% endblock %}
+{% block body %}
+<h1>{{ title }}</h1>
+<p>{{ welcome_text }}</p>
+{% endblock %}
diff --git a/web/template/templates/layout.jinja b/web/template/templates/layout.jinja
new file mode 100644
index 0000000..02232bc
--- /dev/null
+++ b/web/template/templates/layout.jinja
@@ -0,0 +1,17 @@
+<!doctype html>
+<html>
+ <link href="/assets/css/styles.css" rel="stylesheet" type="text/css">
+ <head><title>{% block title %}Website Name{% endblock %}</title></head>
+ <body>
+ <nav>
+ <ul>
+ <li><a href="/">Home</a></li>
+ <li><a href="/content">Content</a></li>
+ <li><a href="/about">About</a></li>
+ </ul>
+ </nav>
+ <h1><h1>Hello, World web =]</h1>
+ <p>Template form https://ijanc.org</p>
+ {% block body %}{% endblock %}
+ </body>
+</html>