Expand description
§rustapi-view
Template rendering support for the RustAPI framework using Tera templates.
This crate provides server-side HTML rendering with type-safe template contexts, layout inheritance, and development-friendly features like auto-reload.
§Features
- Tera Templates: Full Tera template engine support with filters, macros, and inheritance
- Type-Safe Context: Build template context from Rust structs via serde
- Auto-Reload: Development mode can auto-reload templates on change
- Response Types:
View<T>response type for rendering templates - Layout Support: Template inheritance with blocks
§Quick Start
ⓘ
use rustapi_rs::prelude::*;
use rustapi_view::{View, Templates};
use serde::Serialize;
#[derive(Serialize)]
struct HomeContext {
title: String,
user: Option<String>,
}
async fn home(templates: State<Templates>) -> View<HomeContext> {
View::render(&templates, "home.html", HomeContext {
title: "Welcome".to_string(),
user: Some("Alice".to_string()),
})
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let templates = Templates::new("templates/**/*.html")?;
RustApi::new()
.state(templates)
.route("/", get(home))
.run("127.0.0.1:8080")
.await
}Modules§
- prelude
- Prelude module for convenient imports
Structs§
- Context
- The struct that holds the context of a template rendering.
- Context
Builder - Builder for constructing template context
- Templates
- Template engine wrapper providing thread-safe template rendering
- Templates
Config - Configuration for the template engine
- View
- A response that renders a template with a context
Enums§
- View
Error - Error type for view/template operations