Expand description
Context injection for template rendering.
This module provides types for injecting additional context objects into templates beyond the handler’s serialized data. This enables templates to access utilities, formatters, and runtime-computed values that cannot be represented as JSON.
§Overview
The context injection system has two main components:
RenderContext: Information available at render time (output mode, terminal width, theme, etc.)ContextProvider: Trait for objects that can produce context values, either statically or dynamically based onRenderContext
§Use Cases
- Table formatters: Inject
TabularFormatterinstances with resolved terminal width - Terminal info: Provide
terminal.width,terminal.is_ttyto templates - Environment: Expose environment variables or paths
- User preferences: Date formats, timezone, locale
- Utilities: Custom formatters, validators callable from templates
§Example
ⓘ
use standout::context::{RenderContext, ContextProvider};
use minijinja::value::Object;
use std::sync::Arc;
// A simple context object
struct TerminalInfo {
width: usize,
is_tty: bool,
}
impl Object for TerminalInfo {
fn get_value(self: &Arc<Self>, key: &minijinja::Value) -> Option<minijinja::Value> {
match key.as_str()? {
"width" => Some(minijinja::Value::from(self.width)),
"is_tty" => Some(minijinja::Value::from(self.is_tty)),
_ => None,
}
}
}
// Create a dynamic provider using a closure
let provider = |ctx: &RenderContext| TerminalInfo {
width: ctx.terminal_width.unwrap_or(80),
is_tty: ctx.output_mode == OutputMode::Term,
};Structs§
- Context
Registry - Storage for context entries, supporting both static and dynamic providers.
- Render
Context - Information available at render time for dynamic context providers.
- Static
Provider - A static context provider that always returns the same value.
Traits§
- Context
Provider - Trait for types that can provide context objects for template rendering.