diff options
| author | msi | 2025-11-13 14:07:03 -0300 |
|---|---|---|
| committer | msi | 2025-11-13 14:07:03 -0300 |
| commit | 017324a1e98ac2da7812e4fcc06fe67d424e4ffb (patch) | |
| tree | 85e1901cfdfd2b370c73f03796626c9c814b04e9 /web/template/src | |
| parent | b863a55d8a947cfcc6296d34edae4d1365a7725f (diff) | |
| download | templates-017324a1e98ac2da7812e4fcc06fe67d424e4ffb.tar.gz | |
Implement minijinja template
Diffstat (limited to 'web/template/src')
| -rw-r--r-- | web/template/src/main.rs | 41 | ||||
| -rw-r--r-- | web/template/src/router.rs | 86 | ||||
| -rw-r--r-- | web/template/src/state.rs | 21 |
3 files changed, 120 insertions, 28 deletions
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>, +} |