Trait Template

Source
pub trait Template: Display + FastWritable {
    const SIZE_HINT: usize;

    // Required method
    fn render_into_with_values(
        &self,
        writer: &mut dyn Write,
        values: &dyn Values,
    ) -> Result<()>;

    // Provided methods
    fn render(&self) -> Result<String> { ... }
    fn render_with_values(&self, values: &dyn Values) -> Result<String> { ... }
    fn render_into(&self, writer: &mut dyn Write) -> Result<()> { ... }
    fn write_into(&self, writer: &mut dyn Write) -> Result<()> { ... }
    fn write_into_with_values(
        &self,
        writer: &mut dyn Write,
        values: &dyn Values,
    ) -> Result<()> { ... }
}
Expand description

Main Template trait; implementations are generally derived

If you need an object-safe template, use DynTemplate.

§Rendering performance

When rendering a askama template, you should prefer the methods

over .to_string() or format!(). While .to_string() and format!() give you the same result, they generally perform much worse than askama’s own methods, because fmt::Write uses dynamic methods calls instead of monomorphised code. On average, expect .to_string() to be 100% to 200% slower than .render().

Required Associated Constants§

Source

const SIZE_HINT: usize

Provides a rough estimate of the expanded length of the rendered template. Larger values result in higher memory usage but fewer reallocations. Smaller values result in the opposite. This value only affects render. It does not take effect when calling render_into, write_into, the fmt::Display implementation, or the blanket ToString::to_string implementation.

Required Methods§

Source

fn render_into_with_values( &self, writer: &mut dyn Write, values: &dyn Values, ) -> Result<()>

Renders the template to the given writer fmt buffer with provided Values.

§Errors

It internally uses the core::fmt::Write trait so it can fail and return Err for the same reasons. For other potential errors, please take a look at the Error enum variants documentation.

Provided Methods§

Source

fn render(&self) -> Result<String>

Helper method which allocates a new String and renders into it.

§Errors

It internally uses the core::fmt::Write trait so it can fail and return Err for the same reasons. For other potential errors, please take a look at the Error enum variants documentation.

Source

fn render_with_values(&self, values: &dyn Values) -> Result<String>

Helper method which allocates a new String and renders into it with provided Values.

§Errors

It internally uses the core::fmt::Write trait so it can fail and return Err for the same reasons. For other potential errors, please take a look at the Error enum variants documentation.

Source

fn render_into(&self, writer: &mut dyn Write) -> Result<()>

Renders the template to the given writer fmt buffer.

§Errors

It internally uses the core::fmt::Write trait so it can fail and return Err for the same reasons. For other potential errors, please take a look at the Error enum variants documentation.

Source

fn write_into(&self, writer: &mut dyn Write) -> Result<()>

Renders the template to the given writer io buffer.

§Errors

It internally uses the std::io::Write trait so it can fail and return Err for the same reasons. For other potential errors, please take a look at the Error enum variants documentation.

Source

fn write_into_with_values( &self, writer: &mut dyn Write, values: &dyn Values, ) -> Result<()>

Renders the template to the given writer io buffer with provided Values.

§Errors

It internally uses the std::io::Write trait so it can fail and return Err for the same reasons. For other potential errors, please take a look at the Error enum variants documentation.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T: Template + ?Sized> Template for &T

Source§

const SIZE_HINT: usize = T::SIZE_HINT

Source§

fn render(&self) -> Result<String>

Source§

fn render_with_values(&self, values: &dyn Values) -> Result<String>

Source§

fn render_into(&self, writer: &mut dyn Write) -> Result<()>

Source§

fn render_into_with_values( &self, writer: &mut dyn Write, values: &dyn Values, ) -> Result<()>

Source§

fn write_into(&self, writer: &mut dyn Write) -> Result<()>

Source§

fn write_into_with_values( &self, writer: &mut dyn Write, values: &dyn Values, ) -> Result<()>

Implementors§