Skip to main content

ContextProvider

Trait ContextProvider 

Source
pub trait ContextProvider: Send + Sync {
    // Required method
    fn provide(&self, ctx: &RenderContext<'_>) -> Value;
}
Expand description

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

Context providers are called at render time to produce objects that will be available in templates. They receive a RenderContext with information about the current render environment.

§Static vs Dynamic Providers

  • Static providers: Return the same object regardless of context
  • Dynamic providers: Use context to configure the returned object

§Implementing for Closures

A blanket implementation is provided for closures, making it easy to create dynamic providers:

use standout::context::{RenderContext, ContextProvider};

// Closure-based provider
let provider = |ctx: &RenderContext| MyObject {
    width: ctx.terminal_width.unwrap_or(80),
};

§Thread Safety

All context providers must be Send + Sync to support concurrent rendering.

Required Methods§

Source

fn provide(&self, ctx: &RenderContext<'_>) -> Value

Produce a context object for the given render context.

The returned value will be made available in templates under the name specified when registering the provider.

Implementors§

Source§

impl ContextProvider for StaticProvider

Source§

impl<F> ContextProvider for F
where F: Fn(&RenderContext<'_>) -> Value + Send + Sync,

Blanket implementation for closures that return values convertible to minijinja::Value.