Crate rustapi_view

Crate rustapi_view 

Source
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.
ContextBuilder
Builder for constructing template context
Templates
Template engine wrapper providing thread-safe template rendering
TemplatesConfig
Configuration for the template engine
View
A response that renders a template with a context

Enums§

ViewError
Error type for view/template operations