@@ -23,7 +23,6 @@ Create a `templates` directory in your project root.
2323<head >
2424 <meta charset =" UTF-8" >
2525 <title >{% block title %}My App{% endblock %}</title >
26- <link rel =" stylesheet" href =" /assets/style.css" >
2726</head >
2827<body >
2928 <nav >
@@ -36,7 +35,7 @@ Create a `templates` directory in your project root.
3635 </main >
3736
3837 <footer >
39- © ; 2025 RustAPI
38+ © ; 2026 RustAPI
4039 </footer >
4140</body >
4241</html >
@@ -66,11 +65,11 @@ Create a `templates` directory in your project root.
6665
6766## Handling Requests
6867
69- In your ` main.rs ` , use the ` View ` type and ` Context ` .
68+ In your ` main.rs ` , initialize the ` Templates ` engine and inject it into the application state. Handlers can then extract it using ` State<Templates> ` .
7069
7170``` rust,no_run
7271use rustapi_rs::prelude::*;
73- use rustapi_view::{View, Context };
72+ use rustapi_view::{View, Templates };
7473use serde::Serialize;
7574
7675#[derive(Serialize)]
@@ -79,35 +78,43 @@ struct User {
7978 is_admin: bool,
8079}
8180
82- #[rustapi_rs::get("/")]
83- async fn index() -> View {
84- // 1. Create context
85- let mut ctx = Context::new();
86-
87- // 2. Insert data
88- ctx.insert("app_name", "My Awesome App");
81+ #[derive(Serialize)]
82+ struct HomeContext {
83+ app_name: String,
84+ user: User,
85+ items: Vec<String>,
86+ }
8987
90- let user = User {
91- name: "Alice".to_string(),
92- is_admin: true,
88+ #[rustapi_rs::get("/")]
89+ async fn index(templates: State<Templates>) -> View<HomeContext> {
90+ let context = HomeContext {
91+ app_name: "My Awesome App".to_string(),
92+ user: User {
93+ name: "Alice".to_string(),
94+ is_admin: true,
95+ },
96+ items: vec!["Apple".to_string(), "Banana".to_string(), "Cherry".to_string()],
9397 };
94- ctx.insert("user", &user);
9598
96- ctx.insert("items", &vec!["Apple", "Banana", "Cherry"]);
97-
98- // 3. Render template
99- // RustAPI automatically loads templates from the "templates" directory
100- View::new("index.html", ctx)
99+ // Render the "index.html" template with the context
100+ View::render(&templates, "index.html", context).await
101101}
102102
103103#[tokio::main]
104- async fn main() {
105- // No special setup needed for View, it's auto-configured if the crate is present
106- // and the "templates" directory exists.
107- let app = RustApi::new().route("/", get(index));
104+ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
105+ // 1. Initialize Template Engine
106+ // Loads all .html files from the "templates" directory
107+ let templates = Templates::new("templates/**/*.html")?;
108+
109+ // 2. Add to State
110+ let app = RustApi::new()
111+ .state(templates)
112+ .route("/", get(index));
108113
109114 println!("Listening on http://localhost:3000");
110115 app.run("0.0.0.0:3000").await.unwrap();
116+
117+ Ok(())
111118}
112119```
113120
@@ -119,12 +126,11 @@ In **Release** mode (`cargo run --release`), templates are compiled and cached f
119126
120127## Asset Serving
121128
122- To serve CSS, JS, and images, use ` ServeDir ` from ` tower-http ` (re-exported or available via ` rustapi-extras ` if configured, or just standard tower) .
129+ To serve CSS, JS, and images, use ` serve_static ` on the ` RustApi ` builder .
123130
124131``` rust,ignore
125- use tower_http::services::ServeDir;
126-
127132let app = RustApi::new()
133+ .state(templates)
128134 .route("/", get(index))
129- .nest_service ("/assets", ServeDir::new( "assets"));
135+ .serve_static ("/assets", "assets"); // Serves files from ./assets at /assets
130136```
0 commit comments