diff options
author | Jacob Rothstein <hi@jbr.me> | 2020-07-09 21:06:03 -0700 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2020-07-14 09:46:12 +0200 |
commit | 29cb90bceb59d9dbea94342dfdd8136e1ef494e6 (patch) | |
tree | 8d4d63527fbaa039ccf7f0982ff8849c238e68d3 /askama_tide | |
parent | 0a6284aa91e1eb8da81d7b317fb539653e519d0a (diff) | |
download | askama-29cb90bceb59d9dbea94342dfdd8136e1ef494e6.tar.gz askama-29cb90bceb59d9dbea94342dfdd8136e1ef494e6.tar.bz2 askama-29cb90bceb59d9dbea94342dfdd8136e1ef494e6.zip |
askama tide
Diffstat (limited to '')
-rw-r--r-- | askama_tide/Cargo.toml | 18 | ||||
l--------- | askama_tide/LICENSE-APACHE | 1 | ||||
l--------- | askama_tide/LICENSE-MIT | 1 | ||||
-rw-r--r-- | askama_tide/README.md | 9 | ||||
-rw-r--r-- | askama_tide/src/lib.rs | 32 | ||||
-rw-r--r-- | askama_tide/templates/hello.html | 1 | ||||
-rw-r--r-- | askama_tide/tests/tide.rs | 29 |
7 files changed, 91 insertions, 0 deletions
diff --git a/askama_tide/Cargo.toml b/askama_tide/Cargo.toml new file mode 100644 index 0000000..46179a5 --- /dev/null +++ b/askama_tide/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "askama_tide" +version = "0.1.0" +authors = ["Jacob Rothstein <hi@jbr.me>"] +edition = "2018" +description = "Tide integration for Askama templates" +keywords = ["markup", "template", "jinja2", "html", "tide", "http-types"] +homepage = "https://github.com/djc/askama" +repository = "https://github.com/djc/askama" +documentation = "https://docs.rs/askama" +license = "MIT OR Apache-2.0" +workspace = ".." +readme = "README.md" + +[dependencies] +askama = { version = "0.10", path = "../askama", features = ["with-tide"] } +tide = "0.11" +async-std = { version = "1.6.0", features = ["unstable", "attributes"] } diff --git a/askama_tide/LICENSE-APACHE b/askama_tide/LICENSE-APACHE new file mode 120000 index 0000000..965b606 --- /dev/null +++ b/askama_tide/LICENSE-APACHE @@ -0,0 +1 @@ +../LICENSE-APACHE
\ No newline at end of file diff --git a/askama_tide/LICENSE-MIT b/askama_tide/LICENSE-MIT new file mode 120000 index 0000000..76219eb --- /dev/null +++ b/askama_tide/LICENSE-MIT @@ -0,0 +1 @@ +../LICENSE-MIT
\ No newline at end of file diff --git a/askama_tide/README.md b/askama_tide/README.md new file mode 100644 index 0000000..05aa275 --- /dev/null +++ b/askama_tide/README.md @@ -0,0 +1,9 @@ +# askama_tide: Askama integration with tide + +[![Documentation](https://docs.rs/askama_tide/badge.svg)](https://docs.rs/askama_tide/) +[![Latest version](https://img.shields.io/crates/v/askama_tide.svg)](https://crates.io/crates/askama_tide) +[![Build Status](https://github.com/djc/askama/workflows/CI/badge.svg)](https://github.com/djc/askama/actions?query=workflow%3ACI) +[![Chat](https://badges.gitter.im/gitterHQ/gitter.svg)](https://gitter.im/djc/askama) + +Integration of the [Askama](https://github.com/djc/askama) templating engine in +code building on the tide web framework. diff --git a/askama_tide/src/lib.rs b/askama_tide/src/lib.rs new file mode 100644 index 0000000..0458348 --- /dev/null +++ b/askama_tide/src/lib.rs @@ -0,0 +1,32 @@ +pub use askama; +pub use tide; + +use askama::*; +use tide::{http::Mime, Body, Response}; + +pub fn try_into_body<T: Template>(t: &T, ext: &str) -> Result<Body> { + let string = t.render()?; + let mut body = Body::from_string(string); + + if let Some(mime) = Mime::from_extension(ext) { + body.set_mime(mime); + } + + Ok(body) +} + +pub fn into_response<T: Template>(t: &T, ext: &str) -> Response { + match try_into_body(t, ext) { + Ok(body) => { + let mut response = Response::new(200); + response.set_body(body); + response + } + + Err(e) => { + let mut response = Response::new(500); + response.set_body(e.to_string()); + response + } + } +} diff --git a/askama_tide/templates/hello.html b/askama_tide/templates/hello.html new file mode 100644 index 0000000..8149be7 --- /dev/null +++ b/askama_tide/templates/hello.html @@ -0,0 +1 @@ +Hello, {{ name }}! diff --git a/askama_tide/tests/tide.rs b/askama_tide/tests/tide.rs new file mode 100644 index 0000000..6fa1418 --- /dev/null +++ b/askama_tide/tests/tide.rs @@ -0,0 +1,29 @@ +use askama::Template; +use async_std::prelude::*; +use std::convert::TryInto; +use tide::{http::mime::HTML, Body, Response}; + +#[derive(Template)] +#[template(path = "hello.html")] +struct HelloTemplate<'a> { + name: &'a str, +} + +#[async_std::test] +async fn template_to_response() { + let mut res: Response = HelloTemplate { name: "world" }.into(); + assert_eq!(res.status(), 200); + assert_eq!(res.content_type(), Some(HTML)); + + let res: &mut tide::http::Response = res.as_mut(); + assert_eq!(res.body_string().await.unwrap(), "Hello, world!"); +} + +#[async_std::test] +async fn template_to_body() { + let mut body: Body = HelloTemplate { name: "world" }.try_into().unwrap(); + assert_eq!(body.mime(), &HTML); + let mut body_string = String::new(); + body.read_to_string(&mut body_string).await.unwrap(); + assert_eq!(body_string, "Hello, world!"); +} |