Skip to content
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

Add {% break %} and {% continue %} #519

Merged
merged 2 commits into from
Aug 30, 2021
Merged

Add {% break %} and {% continue %} #519

merged 2 commits into from
Aug 30, 2021

Conversation

Kijewski
Copy link
Collaborator

This PR adds {% break %} and {% continue %} statements to break out
of a loop, or continue with the next element of the iterator.

@djc
Copy link
Owner

djc commented Jul 19, 2021

This looks like a nice straightforward implementation, but it looks like this would not throw any error if {% break %} or {% continue %} are added outside of a loop context -- I think it probably should. Any ideas? (I think it might be easier to do it at the codegen stage than in the parser.)

@Kijewski
Copy link
Collaborator Author

I implemented the check inside of the parsing step. It was easier to implement, and it has the added bonus that the user gets informed about the exact position of the offending {%break%}.

This was the first time I had to use a Cell. Does this mean I can call myself a Rust programmer now? ;)

@djc
Copy link
Owner

djc commented Aug 5, 2021

Why do we need to use Cell here? Could we just pass &mut State<'a> around here? (Also I think we could pass State<'_> rather than State<'a>, would you mind checking that?)

(Here, too, I wonder: do you have use cases for these constructs?)

@djc
Copy link
Owner

djc commented Aug 5, 2021

Note that Jinja does not allow this:

Unlike in Python, it’s not possible to break or continue in a loop.

@Kijewski
Copy link
Collaborator Author

Kijewski commented Aug 5, 2021

Replaced State<'a> with State<'_>. I didn't think much about it because all the functions already used lifetime argument, and replaced Syntax<'a> did so, too.

State cannot be passed mutable because it's captured by multiple closures.

Here, too, I wonder: do you have use cases for these constructs?

I used it before in Flask apps. You don't have to use break or continue in templates, since you can always put more logic into the Rust code and less in the template code, but I guess that's a matter of taste.

Note that Jinja does not allow this:

Unlike in Python, it’s not possible to break or continue in a loop.

After 13 years, can we call an extension part of the standard? :)

@djc
Copy link
Owner

djc commented Aug 30, 2021

If you rebase this, I'm happy to merge it. Sorry for the long review cycle!

This PR adds `{% break %}` and `{% continue %}` statements to break out
of a loop, or continue with the next element of the iterator.
@Kijewski
Copy link
Collaborator Author

No problem. Rebased.

@djc
Copy link
Owner

djc commented Aug 30, 2021

Thanks!

@djc djc merged commit b93ce46 into djc:main Aug 30, 2021
@Kijewski Kijewski deleted the pr-for-break branch August 30, 2021 21:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants