[][src]Trait tide_handlebars::TideHandlebarsExt

pub trait TideHandlebarsExt {
    fn render_body<T>(&self, template_name: &str, context: &T) -> Result<Body>
    where
        T: Serialize
;
fn render_body_ext<T>(
        &self,
        template_name: &str,
        context: &T,
        extension: &str
    ) -> Result<Body>
    where
        T: Serialize
;
fn render_response<T>(&self, template_name: &str, context: &T) -> Result
    where
        T: Serialize
;
fn render_response_ext<T>(
        &self,
        template_name: &str,
        context: &T,
        extension: &str
    ) -> Result
    where
        T: Serialize
; }

This extension trait adds two methods to handlebars::Handlebars: render_response and render_body

Required methods

fn render_body<T>(&self, template_name: &str, context: &T) -> Result<Body> where
    T: Serialize

render_body returns a fully-rendered tide::Body with mime type set based on the template name file extension using the logic at tide::http::Mime::from_extension. This will return an Err variant if the render was unsuccessful.

use handlebars::Handlebars;
use tide_handlebars::prelude::*;
use std::collections::BTreeMap;
let mut handlebars = Handlebars::new();
    handlebars
    .register_template_file("simple.html", "./tests/templates/simple.html")
    .unwrap();

let mut data0 = BTreeMap::new();
data0.insert("title".to_string(), "hello tide!".to_string());
let mut body = handlebars.render_body("simple.html", &data0).unwrap();
assert_eq!(body.mime(), &tide::http::mime::HTML);

fn render_body_ext<T>(
    &self,
    template_name: &str,
    context: &T,
    extension: &str
) -> Result<Body> where
    T: Serialize

render_body_ext returns a fully-rendered tide::Body with mime type set based on the extension using the logic at tide::http::Mime::from_extension. This will return an Err variant if the render was unsuccessful.

use handlebars::Handlebars;
use tide_handlebars::prelude::*;
use std::collections::BTreeMap;
let mut handlebars = Handlebars::new();
    handlebars
    .register_template_file("simple.hbs", "./tests/templates/simple.hbs")
    .unwrap();

let mut data0 = BTreeMap::new();
data0.insert("title".to_string(), "hello tide!".to_string());
let mut body = handlebars.render_body_ext("simple.hbs", &data0, "html").unwrap();
assert_eq!(body.mime(), &tide::http::mime::HTML);

fn render_response<T>(&self, template_name: &str, context: &T) -> Result where
    T: Serialize

render_response returns a tide Response with a body rendered with render_body. This will return an Err variant if the render was unsuccessful.

use handlebars::Handlebars;
use tide_handlebars::prelude::*;
use std::collections::BTreeMap;
let mut handlebars = Handlebars::new();
handlebars
    .register_template_file("simple.html", "./tests/templates/simple.html")
    .unwrap();
let mut data0 = BTreeMap::new();
data0.insert("title".to_string(), "hello tide!".to_string());
let mut response = handlebars.render_response("simple.html", &data0).unwrap();
assert_eq!(response.content_type(), Some(tide::http::mime::HTML));

fn render_response_ext<T>(
    &self,
    template_name: &str,
    context: &T,
    extension: &str
) -> Result where
    T: Serialize

render_response_ext returns a tide Response with a body rendered with render_body. This will return an Err variant if the render was unsuccessful.

use handlebars::Handlebars;
use tide_handlebars::prelude::*;
use std::collections::BTreeMap;
let mut handlebars = Handlebars::new();
handlebars
    .register_template_file("simple.hbs", "./tests/templates/simple.hbs")
    .unwrap();
let mut data0 = BTreeMap::new();
data0.insert("title".to_string(), "hello tide!".to_string());
let mut response = handlebars.render_response_ext("simple.hbs", &data0, "html").unwrap();
assert_eq!(response.content_type(), Some(tide::http::mime::HTML));
Loading content...

Implementations on Foreign Types

impl<'_> TideHandlebarsExt for Handlebars<'_>[src]

Loading content...

Implementors

Loading content...