Skip to main content

Module context

Module context 

Source
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:

  1. RenderContext: Information available at render time (output mode, terminal width, theme, etc.)
  2. ContextProvider: Trait for objects that can produce context values, either statically or dynamically based on RenderContext

§Use Cases

  • Table formatters: Inject TabularFormatter instances with resolved terminal width
  • Terminal info: Provide terminal.width, terminal.is_tty to 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§

ContextRegistry
Storage for context entries, supporting both static and dynamic providers.
RenderContext
Information available at render time for dynamic context providers.
StaticProvider
A static context provider that always returns the same value.

Traits§

ContextProvider
Trait for types that can provide context objects for template rendering.