Rocket tries to be type-safe. From the home page https://rocket.rs: "From request to response Rocket ensures that your types mean something." Currently, the template systems included (handlebars, tera) are not type safe and can result in run time errors. An example of type safe template system is: https://github.com/djc/askama
Rocket has already supports two non type safe template systems in contrib, a type safe version would be more in the spirit of Rocket.
I love rust safety, but I think it is not necessary in templates. It may add some unnecessary complexity.
The main problem is that the askama crate is not stable. There will be some maintenance work added when upgrading a version.
Because Askama will generate Rust code from your template files, the crate will need to be recompiled when your templates change.
there is a refresh option although it may be difficult to make it play nice with rocket
Although it would be nice to have optional type-safe templates, I think there are more urgent issues.
Tera had a discussion about making the templates type safe here: https://github.com/Keats/tera/issues/24
There is also maud https://github.com/lfairy/maud/blob/master/README.md
Rocket's contrib::Templates use serde to generify Template data for Hbars and Tera. You would need to implement Serialize for all your Askama / Maud template objects (unless they already do that) and then create them in your routes, serialize them, pass them through the Template API, and finally deserialzie them in the handlers to be consumed by the rendering engines.
That is a lot of overhead just to plug template engines _meant_ to be structured into a very generic template API. The better answer is for anyone interested to just write rocket-askama or rocket-maud wrapper crates, where you just glue the two together by providing a fairing and Responder for your template engine.
Edit: Maud already supports Rocket. You simply import Maud::Markup and that implements Rocket's Responder already. It doesn't need contrib integration or a wrapper crate.
I moved to Maud now, seems almost what I want :)
I'd be really excited to see a full-featured, easy to integrate, compile-time checked, and general purpose templating library for Rust that is subsequently integrated into Rocket. Unfortunately none appear to exist at the moment. All of the available options, with the exception of Maud which isn't general purpose, appear to be rather nascent. Until such an option exists, it's difficult to contemplate what such an integration might look like.
That being said, I think something will eventually exist, and I agree that Rocket should support it as a first-class citizen. The approach to incorporating support for a compile-time templating engine will likely differ substantially from what Rocket does now. Whatever form it takes, it should be as easy and usable as what we have now. If anyone wants to experiment, I'd be more than happy to review and advise on potential integration strategies!
@SergioBenitez since yesterday, Askama master includes optional support for Rocket, by implementing Responder for any struct that derives Template.
Test case (which also serves as a good example) here:
https://github.com/djc/askama/blob/master/testing/tests/rocket.rs
Code generation happens here:
https://github.com/djc/askama/blob/master/askama_derive/src/generator.rs#L652
I'd be happy to have you review this integration for potential improvements (the two unwrap() calls in the generated code are obvious candidates); I'm also curious, specifically for Askama's case, what about it makes you consider it "nascent".
@djc I'm confused what kind of integration Rocket could even do with Askama. You build them before the main project - you don't have any inits, so you don't need a fairing or other wrapper init structure in main like the dynamic templates. Rocket itself isn't overriding the build process so it can't automagically inject the Askama template build stage to my knowledge. The types constructed are already Responders, so you can just return them. There is no boilerplate already, so what would Rocket integration look like?
I'd definitely see and support adding a subsection under the Handlebars / Tera template description in the contrib docs saying Askama and Maud are typesafe template systems that support Rocket. Should at least tell users these options exist if they want them.
@Korvox I'm not saying Rocket needs to write any code for Askama integration, I'm merely asking if Sergio (and others) have feedback on the Rocket integration I built into Askama.
Why is maud not mentioned in the docs?
@flavius because those are not in rocket_contrib right now. Terra/Handlebars is the only one supported right now.
FYI, I released a new version of Askama that includes the Rocket integration (but without the unwrap()s); for more, see the changelog.
Askama and other upstream libraries are good choices! We'll leave it up to the broader ecosystem to fill in the gap here.
Most helpful comment
@SergioBenitez since yesterday, Askama master includes optional support for Rocket, by implementing
Responderfor any struct that derivesTemplate.Test case (which also serves as a good example) here:
https://github.com/djc/askama/blob/master/testing/tests/rocket.rs
Code generation happens here:
https://github.com/djc/askama/blob/master/askama_derive/src/generator.rs#L652
I'd be happy to have you review this integration for potential improvements (the two
unwrap()calls in the generated code are obvious candidates); I'm also curious, specifically for Askama's case, what about it makes you consider it "nascent".