Skip to main content

TemplateEngine

Trait TemplateEngine 

Source
pub trait TemplateEngine: Send + Sync {
    // Required methods
    fn name(&self) -> &str;
    fn render(
        &self,
        template: &Template,
        context: &TemplateContext,
        shipper: &Arc<dyn TemplateShipper>,
    ) -> Result<String, String>;

    // Provided methods
    fn artifacts(
        &self,
        template: &Template,
        context: &TemplateContext,
        shipper: &Arc<dyn TemplateShipper>,
    ) -> Result<Vec<TemplateArtifact>, String> { ... }
    fn render_text(
        &self,
        template: &Template,
        text: &str,
        place: Option<&str>,
        context: &TemplateContext,
        shipper: &Arc<dyn TemplateShipper>,
    ) -> Result<String, String> { ... }
}
Expand description

A template engine: the code that applies a compose template written in its language to a TemplateContext.

A feature contributes one to ENGINES_SLOT; a compose template refers to its template engine by name (Template::engine).

Required Methods§

Source

fn name(&self) -> &str

Returns the engine’s name, by which a compose template refers to it.

Source

fn render( &self, template: &Template, context: &TemplateContext, shipper: &Arc<dyn TemplateShipper>, ) -> Result<String, String>

Returns the text of the compose template template applied to context.

§Errors

Returns a text, for a person to read, that says why the compose template cannot be applied: for instance its text does not compile, or it refers to a value that context does not provide. The stage reports it in TemplateFailed.

An engine whose language includes other files reads a path written without a root beside the place of the text that writes it (Template::source).

An engine whose language ships a file hands it to shipper and writes the path it returns. The pointer is shared, so that the engine can keep it where a function of its language must own what it holds.

Provided Methods§

Source

fn artifacts( &self, template: &Template, context: &TemplateContext, shipper: &Arc<dyn TemplateShipper>, ) -> Result<Vec<TemplateArtifact>, String>

Returns the files the compose template template ships when it is applied to context, which the composer names and writes beside the entry artifact, in this order, and whose paths it then gives to render in TemplateContext::artifacts. The composer calls it once per application, before it names the files, so context holds no path yet: its artifacts is empty.

The default returns the compose template’s own list (Template::artifacts). An engine whose compose templates choose their files when they are applied returns them here. shipper is the handle of the same application that render receives, through which the engine reports a warning while it reads the files.

§Errors

Returns a text, for a person to read, that says why the files cannot be given. The stage reports it in TemplateFailed, as for render.

Source

fn render_text( &self, template: &Template, text: &str, place: Option<&str>, context: &TemplateContext, shipper: &Arc<dyn TemplateShipper>, ) -> Result<String, String>

Applies text, written in this engine’s language under the settings of template (its name, its delimiters), to context, and returns the result. place is the place of text (see the module documentation), None for a text that no file holds. The composer calls it for a file the compose template ships and applies (the fields apply and source of TemplateArtifact::File).

The default applies render to a copy of template whose text is text and whose place is place, which serves an engine that reads the compose template’s text. An engine ships files through shipper as render does.

§Errors

As render.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§