djc / askama

Type-safe, compiled Jinja-like templates for Rust
Apache License 2.0
3.23k stars 212 forks source link

Documentation for `askama_warp` usage #559

Open cmcqueen opened 2 years ago

cmcqueen commented 2 years ago

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.)

vallentin commented 2 years ago

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)

cmcqueen commented 2 years ago

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?

cmcqueen commented 2 years ago

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?

djc commented 2 years ago

Yes, that's exactly right. The closure is also just a function handler, just using a more compact syntax.

cmcqueen commented 2 years ago

Thanks! I'm sure it would be helpful to others to add your example to the documentation, as well as the handler function alternative.

djc commented 2 years ago

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.

Amy7944 commented 6 months ago

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

Amy7944 commented 6 months ago

AmyDGarcia