Trait Template

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

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

    // Provided methods
    fn render(&self) -> Result<String> { ... }
    fn render_with_values(&self, values: &dyn Values) -> Result<String> { ... }
    fn render_into<W: Write + ?Sized>(&self, writer: &mut W) -> Result<()> { ... }
    fn write_into<W: Write + ?Sized>(&self, writer: &mut W) -> Result<()> { ... }
    fn write_into_with_values<W: Write + ?Sized>(
        &self,
        writer: &mut W,
        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<W: Write + ?Sized>( &self, writer: &mut W, values: &dyn Values, ) -> Result<()>

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

Provided Methods§

Source

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

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

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.

Source

fn render_into<W: Write + ?Sized>(&self, writer: &mut W) -> Result<()>

Renders the template to the given writer fmt buffer.

Source

fn write_into<W: Write + ?Sized>(&self, writer: &mut W) -> Result<()>

Renders the template to the given writer io buffer.

Source

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

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

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<W: Write + ?Sized>(&self, writer: &mut W) -> Result<()>

Source§

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

Source§

fn write_into<W: Write + ?Sized>(&self, writer: &mut W) -> Result<()>

Source§

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

Implementors§