diff options
author | cel 🌸 <cel@blos.sm> | 2024-01-24 20:23:18 +0000 |
---|---|---|
committer | cel 🌸 <cel@blos.sm> | 2024-01-24 20:23:18 +0000 |
commit | d3b048544eebf7b8402fb585b4e2531de9b28a33 (patch) | |
tree | 2e697063854d22aff603ba3691c6453ddf5cd87b /askama_poem | |
parent | ef53238ae2ee77becc44bcb0b77c1c4daceca5af (diff) | |
download | askama-main.tar.gz askama-main.tar.bz2 askama-main.zip |
Diffstat (limited to 'askama_poem')
-rw-r--r-- | askama_poem/Cargo.toml | 32 | ||||
l--------- | askama_poem/LICENSE-APACHE | 1 | ||||
l--------- | askama_poem/LICENSE-MIT | 1 | ||||
-rw-r--r-- | askama_poem/README.md | 9 | ||||
-rw-r--r-- | askama_poem/src/lib.rs | 17 | ||||
-rw-r--r-- | askama_poem/templates/hello.html | 1 | ||||
-rw-r--r-- | askama_poem/tests/basic.rs | 23 |
7 files changed, 84 insertions, 0 deletions
diff --git a/askama_poem/Cargo.toml b/askama_poem/Cargo.toml new file mode 100644 index 0000000..20c720b --- /dev/null +++ b/askama_poem/Cargo.toml @@ -0,0 +1,32 @@ +[package] +name = "askama_poem" +version = "0.14.0" +description = "Poem integration for Askama templates" +documentation = "https://docs.rs/askama" +keywords = ["markup", "template", "jinja2", "html"] +categories = ["template-engine"] +homepage = "https://github.com/djc/askama" +repository = "https://github.com/djc/askama" +license = "MIT OR Apache-2.0" +workspace = ".." +readme = "README.md" +edition = "2021" +rust-version = "1.65" + +[dependencies] +poem = { version = "2.0.0", default-features = false } +askama = { version = "0.12", path = "../askama", default-features = false, features = ["with-poem"] } + +[dev-dependencies] +poem = { version = "2.0.0", features = ["test"] } +tokio = "1.0" + +[features] +default = ["askama/default"] +config = ["askama/config"] +humansize = ["askama/humansize"] +markdown = ["askama/markdown"] +num-traits = ["askama/num-traits"] +serde-json = ["askama/serde-json"] +serde-yaml = ["askama/serde-yaml"] +urlencode = ["askama/urlencode"] diff --git a/askama_poem/LICENSE-APACHE b/askama_poem/LICENSE-APACHE new file mode 120000 index 0000000..965b606 --- /dev/null +++ b/askama_poem/LICENSE-APACHE @@ -0,0 +1 @@ +../LICENSE-APACHE
\ No newline at end of file diff --git a/askama_poem/LICENSE-MIT b/askama_poem/LICENSE-MIT new file mode 120000 index 0000000..76219eb --- /dev/null +++ b/askama_poem/LICENSE-MIT @@ -0,0 +1 @@ +../LICENSE-MIT
\ No newline at end of file diff --git a/askama_poem/README.md b/askama_poem/README.md new file mode 100644 index 0000000..0aecdea --- /dev/null +++ b/askama_poem/README.md @@ -0,0 +1,9 @@ +# askama_poem: Askama integration with Poem + +[![Documentation](https://docs.rs/askama_poem/badge.svg)](https://docs.rs/askama_poem/) +[![Latest version](https://img.shields.io/crates/v/askama_poem.svg)](https://crates.io/crates/askama_poem) +[![Build Status](https://github.com/djc/askama/workflows/CI/badge.svg)](https://github.com/djc/askama/actions?query=workflow%3ACI) +[![Chat](https://img.shields.io/discord/976380008299917365?logo=discord)](https://discord.gg/ZucwjE6bmT) + +Integration of the [Askama](https://github.com/djc/askama) templating engine in +code building on the poem web framework. diff --git a/askama_poem/src/lib.rs b/askama_poem/src/lib.rs new file mode 100644 index 0000000..de69e09 --- /dev/null +++ b/askama_poem/src/lib.rs @@ -0,0 +1,17 @@ +#![forbid(unsafe_code)] +#![deny(elided_lifetimes_in_paths)] +#![deny(unreachable_pub)] + +pub use askama::*; +use poem::http::StatusCode; +pub use poem::{web::IntoResponse, Response}; + +pub fn into_response<T: Template>(t: &T) -> Response { + match t.render() { + Ok(body) => Response::builder() + .status(StatusCode::OK) + .content_type(T::MIME_TYPE) + .body(body), + Err(_) => StatusCode::INTERNAL_SERVER_ERROR.into(), + } +} diff --git a/askama_poem/templates/hello.html b/askama_poem/templates/hello.html new file mode 100644 index 0000000..8149be7 --- /dev/null +++ b/askama_poem/templates/hello.html @@ -0,0 +1 @@ +Hello, {{ name }}! diff --git a/askama_poem/tests/basic.rs b/askama_poem/tests/basic.rs new file mode 100644 index 0000000..406c45f --- /dev/null +++ b/askama_poem/tests/basic.rs @@ -0,0 +1,23 @@ +use askama::Template; +use poem::{handler, test::TestClient, Route}; + +#[derive(Template)] +#[template(path = "hello.html")] +struct HelloTemplate<'a> { + name: &'a str, +} + +#[handler] +fn hello() -> HelloTemplate<'static> { + HelloTemplate { name: "world" } +} + +#[tokio::test] +async fn test_poem() { + let app = Route::new().at("/", hello); + let cli = TestClient::new(app); + + let resp = cli.get("/").send().await; + resp.assert_status_is_ok(); + resp.assert_text("Hello, world!").await; +} |