tide-handlebars

Crates.iotide-handlebars
lib.rstide-handlebars
version0.9.0
sourcesrc
created_at2020-07-13 12:41:15.31766
updated_at2021-02-01 23:32:40.619421
descriptionA crate to simplify working with Tide and Handlebars
homepagehttps://github.com/no9/tide-handlebars
repositoryhttps://github.com/no9/tide-handlebars
max_upload_size
id264696
size76,917
Anton Whalley (No9)

documentation

https://docs.rs/tide-handlebars

README

tide-handlebars

This crate exposes an extension trait that adds four functions to handlebars::Handlebars:

  • render_response - Render the template and return a tide response using the template name to assume the content type e.g. template.html would set the content type to be text/html

  • render_body- Render the template and return a tide body using the template name to assume the content type e.g. template defaults the content type to be text/plain

  • render_response_ext - Render the template and return a tide response specifying the file extension explicitly e.g. "html" would set the content type to be text/html

  • render_body_ext - Render the template and return a tide body using the template extension to assume the content type

Documentation


Crates.io version docs.rs docs Build

usage

use handlebars::Handlebars;
use std::collections::BTreeMap;
use tide_handlebars::prelude::*;

struct HandlebarsEngine {
    registry: Handlebars<'static>,
}

#[async_std::main]
async fn main() -> tide::Result<()> {
    tide::log::start();

    let mut engine = HandlebarsEngine {
        registry: Handlebars::new(),
    };

    engine
        .registry
        .register_template_file("simple.html", "./examples/templates/simple.html")
        .unwrap();

    let mut app = tide::with_state(engine);
    app.at("/:name")
        .get(|req: tide::Request<HandlebarsEngine>| async move {
            let hb = &req.state().registry;
            let name: String = req.param("name")?;
            let mut data0 = BTreeMap::new();
            data0.insert("name".to_string(), name);
            Ok(hb.render_response("simple.html", &data0)?)
        });

    app.listen("127.0.0.1:8080").await?;

    Ok(())
}

See the main handlebars repo for full details on handlebars usage.

Commit count: 19

cargo fmt