Skip to main content

TemplateEngine

Trait TemplateEngine 

Source
pub trait TemplateEngine: Send + Sync {
    // Required methods
    fn render_template(
        &self,
        template: &str,
        data: &Value,
    ) -> Result<String, RenderError>;
    fn add_template(
        &mut self,
        name: &str,
        source: &str,
    ) -> Result<(), RenderError>;
    fn render_named(
        &self,
        name: &str,
        data: &Value,
    ) -> Result<String, RenderError>;
    fn has_template(&self, name: &str) -> bool;
    fn render_with_context(
        &self,
        template: &str,
        data: &Value,
        context: HashMap<String, Value>,
    ) -> Result<String, RenderError>;
    fn supports_includes(&self) -> bool;
    fn supports_filters(&self) -> bool;
    fn supports_control_flow(&self) -> bool;
}
Expand description

A template engine that can render templates with data.

This trait abstracts over the template rendering backend, allowing different implementations (e.g., MiniJinja, simple string substitution).

Template engines handle:

  • Template compilation and caching
  • Variable substitution
  • Template logic (loops, conditionals) - if supported
  • Custom filters and functions - if supported

Required Methods§

Source

fn render_template( &self, template: &str, data: &Value, ) -> Result<String, RenderError>

Renders a template string with the given data.

This compiles and renders the template in one step. For repeated rendering of the same template, use add_template and render_named.

Source

fn add_template(&mut self, name: &str, source: &str) -> Result<(), RenderError>

Adds a named template to the engine.

The template is compiled and cached for later use via render_named.

Source

fn render_named(&self, name: &str, data: &Value) -> Result<String, RenderError>

Renders a previously registered template.

The template must have been added via add_template.

Source

fn has_template(&self, name: &str) -> bool

Checks if a template with the given name exists.

Source

fn render_with_context( &self, template: &str, data: &Value, context: HashMap<String, Value>, ) -> Result<String, RenderError>

Renders a template with additional context values merged in.

The context values are merged with the serialized data. If there are key conflicts, data takes precedence.

Source

fn supports_includes(&self) -> bool

Whether this engine supports template includes ({% include %}).

Source

fn supports_filters(&self) -> bool

Whether this engine supports filters ({{ value | filter }}).

Source

fn supports_control_flow(&self) -> bool

Whether this engine supports control flow ({% for %}, {% if %}).

Implementors§