Skip to main content

ToTemplate

Trait ToTemplate 

Source
pub trait ToTemplate<Ctx: ?Sized = ()> {
    // Required method
    fn to_template(&self) -> impl Template<Ctx> + '_;
}
Expand description

A trait to convert a type to a Template. Used when deriving Template.

Useful to implement on structs.

#[derive(Template)]
struct Greet<'a> {
    name: &'a str,
}

impl ToTemplate for Greet<'_> {
    fn to_template(&self) -> impl Template + '_ {
        templ! {
            Hello, {self.name}!
        }
    }
}

let t = templ! {
    #(Greet { name: "baba" });
};
let html = t.render(&()).unwrap();
assert_eq!(html, "Hello, baba!");

Required Methods§

Source

fn to_template(&self) -> impl Template<Ctx> + '_

Converts this type into the template. This function should be as lightweight as possible.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementations on Foreign Types§

Source§

impl<Ctx: ?Sized, T: ToTemplate<Ctx> + ?Sized> ToTemplate<Ctx> for &T

Source§

fn to_template(&self) -> impl Template<Ctx> + '_

Implementors§