askama icon indicating copy to clipboard operation
askama copied to clipboard

Support rust imports

Open ModProg opened this issue 2 years ago • 4 comments

While you probably won't have a lot of rust code inside templates, having the possibility to import e.g. a trait like Borrow (https://github.com/djc/askama/issues/330#issuecomment-1120411565).

You currently have to use the trait in the surrounding rust file, but when e.g. having super template used across different modules, it would be helpful to allow adding some use's at the top of a template.

{% use std::borrow::Borrow %}

I'm not familiar with the parsing of askama, but it should be possible to parse the use statement with syn and just add them to the expansions.

The only issue could be conflicts, but those should occur very rarely and relatively easy to understand for a rust user.

ModProg avatar May 08 '22 13:05 ModProg

Hmm, I'm not a fan. If Borrow in particular provides a useful workaround for a lot of people we could implicitly import it in generated rendering template functions (but I'm not convinced this is the case, since it hasn't come up before). Otherwise, I'm okay with requiring people to have imports like this in the containing scope of the template context type, where it can be picked up by the template as well.

djc avatar May 09 '22 09:05 djc

Well, there is probably a better solution for the borrow issue than the Borrow trait itself.

This was a more general thing with having some logic that depends on imports in a super template, and everywhere you use that you need to import these things even tho the extending template itself doesn't actually use the imports.

But maybe this is more of a niche problem that does not warrant an actual dedicated implementation.

Maybe a general "header" where you can put any rust statements that get put above the expansion without askama caring about what it is, could work as well.

But not sure if this is something that would get actual use for most people, and templates are obviously not supposed to contain a lot of rust code.

ModProg avatar May 09 '22 13:05 ModProg

Just ran into a case where this would be nice to have myself. I have a nav.html that's included by base.html, and base.html is then extended by various other templates. In nav.html I have a lot of nav items that are only supposed to show up if the user has a certain permission, so my options are to:

A. Use crate::permission::Permission::SomePermission for every single check B. use crate::permission::Permission in every rust file that has templates

It's not too terribly long, so I'm just going with option A, but I can imagine a case where you have something deeper in the tree that needs to be used in a base template where a scoped {% use %} would be very nice.

AndrolGenhald avatar Nov 05 '22 04:11 AndrolGenhald

I would consider remodelling your nav items as type that has the required permission wrapped up in the type, then you can add a method on that that checks the session for that permission.

(I'm still not inclined to add support for imports to the Askama language.)

djc avatar Nov 07 '22 10:11 djc