askama icon indicating copy to clipboard operation
askama copied to clipboard

Mime type

Open benluelo opened this issue 2 years ago • 8 comments

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 {}
}

benluelo avatar Feb 06 '22 20:02 benluelo

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

djc avatar Feb 06 '22 20:02 djc

This is the content of my askama.toml file:

[[escaper]]
path = "::askama::Html"
extensions = ["askama"]

benluelo avatar Feb 06 '22 20:02 benluelo

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.

djc avatar Feb 07 '22 10:02 djc

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"?

Kijewski avatar Feb 07 '22 13:02 Kijewski

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

djc avatar Feb 07 '22 14:02 djc

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.

benluelo avatar Feb 07 '22 14:02 benluelo

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

djc avatar Feb 07 '22 14:02 djc

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

benluelo avatar Feb 07 '22 15:02 benluelo