Trait HandlebarsConnExt

Source
pub trait HandlebarsConnExt {
    // Required methods
    fn assign(
        self,
        key: impl Into<Cow<'static, str>>,
        data: impl Serialize,
    ) -> Self;
    fn render_with(self, template: &str, data: &impl Serialize) -> Self;
    fn render(self, template: &str) -> Self;
    fn assigns(&self) -> Option<&Assigns>;
    fn assigns_mut(&mut self) -> &mut Assigns;
}
Expand description

Extension trait that provides handlebar rendering capabilities to [trillium::Conn]s.

Required Methods§

Source

fn assign(self, key: impl Into<Cow<'static, str>>, data: impl Serialize) -> Self

Registers an “assigns” value on this Conn for use in a template. See example usage at Handlebars::new

Source

fn render_with(self, template: &str, data: &impl Serialize) -> Self

renders a registered template by name with the provided data as assigns. note that this does not use any data accumulated by HandlebarsConnExt::assign

use trillium_handlebars::{HandlebarsHandler, Handlebars, HandlebarsConnExt};

#[derive(serde::Serialize)]
struct User { name: &'static str };

let mut handlebars = Handlebars::new();
handlebars.register_template_string("greet-user", "Hello {{name}}")?;

let handler = (
    HandlebarsHandler::new(handlebars),
    |mut conn: trillium::Conn| async move {
        conn.render_with("greet-user", &User { name: "handlebars" })
    }
);

use trillium_testing::prelude::*;
assert_ok!(get("/").on(&handler), "Hello handlebars");
Source

fn render(self, template: &str) -> Self

renders a registered template, passing any accumulated assigns to the template. See example at Handlebars::new

Source

fn assigns(&self) -> Option<&Assigns>

retrieves a reference to any accumulated assigns on this conn

Source

fn assigns_mut(&mut self) -> &mut Assigns

retrieves a mutable reference to any accumulated assigns on this conn

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 HandlebarsConnExt for Conn

Source§

fn render_with(self, template: &str, data: &impl Serialize) -> Self

Source§

fn assign(self, key: impl Into<Cow<'static, str>>, data: impl Serialize) -> Self

Source§

fn render(self, template: &str) -> Self

Source§

fn assigns(&self) -> Option<&Assigns>

Source§

fn assigns_mut(&mut self) -> &mut Assigns

Implementors§