-
-
Notifications
You must be signed in to change notification settings - Fork 228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Don't force adding "&" to start of collection for TemplateLoop #221
Don't force adding "&" to start of collection for TemplateLoop #221
Conversation
…ollections to be passed directly to a template without needed to create new owned collection.
There's actually a fairly extensive change log here: https://github.com/djc/askama/releases, but maybe it would help to link to it from the README to make it more discoverable? |
Although I agree that this should work, I don't think this is the right direction for a solution. Can you show in more detail what causes the "cannot move out of borrowed context" problem in this case? |
Great, I'll update https://github.com/djc/askama/releases . Definitely not sure if this is the right solution either but will be great to be able to use referenced collections. When I use 0.8 Askama here is what I get: Code:
Template
Askama Output
Compiler Error
|
So now can you put the generated source code in your own source code, comment out the |
Sure, here you go.
Error:
|
Just checking @djc if you had any feedback |
I suppose the problem here is that you want So far I've resisted adding |
I understand not wanting to have to think about "&" in Askama templates. The issue is that I have some long lived collections of strings that are loaded at server start that are used to populate the text for various templates. Askama currently can't loop through collections held by the template struct that are references with lifetime annotations, and not owned by the struct itself. This PR fixes that issue but is breaking. I've been trying to research a way in Rust that we could detect a value is a reference instead of owned at run time. I am going to look into TryFrom but am not sure if that's the right way to go. |
I wonder what we could do here if we add filters to the mix. That way, you could at least write Rust code that adapts your iterator, in Rust code, possibly to some trait defined in askama that doesn't get the mandatory reference treatment. Does that make sense? |
My issue with the current implementation is that it seems Conversely, I get the rational behind this and it's definitely a nice idea to not mutate iterables used in templates. However, I think that trying to implement It would be great to get this resolved because, at least for me, it currently presents quite a barrier to using Askama 😞 |
Currently the TemplateLoop generator always appends
&
to every expression. This is inconvenient as it doesn't allow iteration over collections that are not owned by a struct but are passed by reference.For example today this does not work:
welcome.rs
welcome.html
This code will produce a cannot move out of borrowed context error.
The current work around is:
welcome.rs
The bad thing is that this would be a breaking change as it would require collections that are owned by the template to have the & prefix. I think this should be the correct pattern, I couldn't figure out any other way to check of the template field was a reference or owned.
Also, I noticed there is no CHANGELOG.md file, I can create one if you think it's a good idea.