askama icon indicating copy to clipboard operation
askama copied to clipboard

Mutable local variables

Open imbolc opened this issue 2 years ago • 4 comments

It would be useful to have mutable local variables. With {{ let mut foo }} syntax maybe?

The idea originates in the @djc comment: https://github.com/djc/askama/issues/669#issuecomment-1104853641

imbolc avatar Apr 21 '22 08:04 imbolc

It would be nice if we can do this without explicit syntax. Not sure if that can work...

djc avatar Apr 21 '22 08:04 djc

Wouldn't it surprise rustaceans used to immutable by default variables?

imbolc avatar Apr 21 '22 08:04 imbolc

Hmm, I think that wouldn't be a big deal. Remember, you're in the context of a &self method, so none of the mutability here is externally observable anyway.

djc avatar Apr 21 '22 08:04 djc

Still, with introduction mutability inside templates it could lead to bugs. E.g. let's say we have this structure:

# base.html
{{ let foo = 100 }}
{% include "inner.html" %}

# inner.html
{{ let mut foo = 0 }}
{{ foo += 1 }}
{{ foo }}

Now, let's imagine I forgot or accidentally removed {{ let mut foo = 0 }} from the inner template. With explicit mutability I get compilation error, without it foo == 101 :)

imbolc avatar Apr 21 '22 09:04 imbolc