askama
askama copied to clipboard
Documentation for `askama_warp` usage
I'm starting to use askama with warp.
I'm finding the askama documentation is too terse for beginners like me. Thankfully, I found the blog post Template rendering in Rust - LogRocket Blog, which has been tremendously useful in getting started.
That blog post doesn't use askama_warp
. I assume askama_warp
would simplify the code in that blog post example welcome_handler()
. But, I can't understand how to change that code to use the reply()
function provided by askama_warp
.
It would be very helpful if the askama_warp
documentation would include a hello-world example that serves an HTML page.
(I'm new to the Rust language this year, learning the language, with a background in C, C++, Python. I'm trying to learn the idioms unique to the Rust language, but don't feel like I've worked it out yet.)
Check out the Askama book, specifically the Integrations page. It links to this test in the askama_warp
crate.
That being said, maybe the test/example needs to be improved. Because I actually got the same question from a friend a few weeks ago. He was confused as well, how to combine Askama and Warp.
So let me give you a short example:
Hello {{ name }}
use askama::Template;
use warp::Filter;
#[derive(Template, Clone)]
#[template(path = "index.html")]
struct HelloTemplate {
name: String,
}
#[tokio::main]
async fn main() {
let hello = warp::path("hello")
.and(warp::path::param())
.map(|name: String| HelloTemplate { name });
warp::serve(hello).run(([127, 0, 0, 1], 3030)).await;
}
[dependencies]
tokio = { version = "1", features = ["full"] }
warp = { version = "0.3", default-features = false }
askama = { version = "0.10.5", features = ["with-warp"] }
askama_warp = "0.11.0"
Now do cargo run
and then you can access the page http://localhost:3030/hello/bob
and it will display Hello bob
.
If you're still confused or have additional questions, then feel free to respond.
(I'll look into improving the warp test/example)
Thanks, that is helpful. But, I do have an additional question. Like the blog post I mentioned, which makes a handler function welcome_handler()
, I will probably need to make a handler function to create the result (because loading data into the template is more complex than the simple example).
So, what is the appropriate return type for such a handler function that uses askama_warp
?
I think I may have worked it out. In your example, would I make a function:
async fn hello_handler(name: String) -> impl warp::Reply {
HelloTemplate {
name
}
}
Then .map(|name: String| HelloTemplate { name })
would be replaced with .then(hello_handler)
.
Is that right?
Yes, that's exactly right. The closure is also just a function handler, just using a more compact syntax.
Thanks! I'm sure it would be helpful to others to add your example to the documentation, as well as the handler function alternative.
Seems like that this should be pretty clear to most more experienced Rust users, but if someone submits a PR I'd be happy to review and merge it.
I think I may have worked it out. In your example, would I make a function:
async fn hello_handler(name: String) -> impl warp::Reply { HelloTemplate { name } }
Then
.map(|name: String| HelloTemplate { name })
would be replaced with.then(hello_handler)
.Is that right? Yes. AmyDGarcia
AmyDGarcia