djc / askama

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

Mime type #631

Open benluelo opened 2 years ago

benluelo commented 2 years ago

It should be possible to change the mime type of the templates. I use .askama for my templates and .html for raw html files, and I get content-type: application/octet-stream when using askama_axum. I am using the latest git commit.

askama = { git = "https://github.com/djc/askama.git", features = ["with-axum"] }
askama_axum = { git = "https://github.com/djc/askama.git" }

An example template:

#[derive(Debug, askama::Template)]
#[template(path = "signup.askama")]
pub(crate) struct Template {}

pub(crate) async fn get() -> Template {
    Template {}
}
djc commented 2 years ago

I think you can use the configuration to do this: https://djc.github.io/askama/configuration.html.

benluelo commented 2 years ago

This is the content of my askama.toml file:

[[escaper]]
path = "::askama::Html"
extensions = ["askama"]
djc commented 2 years ago

Ah, sorry; it does like we currently always use the mime_guess crate to determine the MIME type from an extension. I suppose we could add configuration that would let you override the MIME type in the configuration for a particular escaper entry (although it makes that name no longer a great fit). Would you be interested in contributing such a change? You'd want to add a field to the RawEscaper, thread it into Config::escapers and then override TemplateInput::mime_type (and add a test). Should be fairly straightforward.

Kijewski commented 2 years ago

In https://github.com/djc/askama/blob/082a9c2db9bb128956dd70f146af3e0228e4c433/askama_shared/src/input.rs#L225 we strip off Jinja2 extensions. Maybe we could make this list extensible or add ".askama"?

djc commented 2 years ago

That also seems like a decent option, but would require the OP to make their extensions .html.askama, I suppose?

benluelo commented 2 years ago

I think extending the escaper is the best bet. I also think it would be a good idea to have a parameter in the template derive, something like #[template(mime_type = "whatever")] (perhaps only available when used with an integration that supports it?)

As for extending the escaper, what are your thoughts on something like this?

[[alias]]
askama = "html"

Would let you use .askama as a synonym to .html.

djc commented 2 years ago

That would add a bunch of complexity that I'd prefer to avoid.

benluelo commented 2 years ago

Understandable. I will start work on a PR later today for the changes mentioned previously!