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

Implement {% do %} blocks and fix {% set %} block scoping #1030

Merged
merged 6 commits into from
Mar 23, 2023
Merged

Conversation

jasmith-hs
Copy link
Contributor

@jasmith-hs jasmith-hs commented Mar 16, 2023

These are to inline do tags as set blocks are to inline set tags.

{% do %}
  {{ some_macro() }}
  {{ other_macro() }}
{% enddo %}

They run the block of code within their body and then discard the output. This is done without going into a child scope so variables that are set here will persist their values outside of the do block. This is unlike a {% for %} tag or a macro function execution.
This is also supposed to be unlike a set block, which, in jinja, executes in a child scope. I realised that we were not running set blocks in a child scope and so I wanted to fix that to have parity with jinja. However, we were using set blocks in eager execution to be able to execute code, while ignoring the string output. This is useful for {% extends %}, {% from %} and {% import %} as it would allow us to retain any deferred modifications that happened as a result of using those tags without adding any extra output. But this also hinged on the lack of child scoping that set blocks currently use. So the need for a solution where we could ignore the output, keep the current scope, and fix the scoping of set blocks to match their functionality in jinja arose. Implementing do blocks fit the bill perfectly, and we can continue not using a child scope when executing set blocks with that __ignored__ variable name so as to not break any previous eager renders that used that.

Additionally, after realising that deferred macro functions should be put into their own scope #1020, I have been working through another bug the past couple days and found that it's simpler to instead just use the macro function temporary variable functionality I added in #920, which should scope the modifications done in the macro function properly. That's how I realised that the bug with set blocks existed because that didn't work as it was supposed to.

These are like inline do tags, except they evaluate the body and then discard the output rather than just the single expression.
If __ignored__ is the name of the variable, don't use a child scope as that was used to ignore output before
do blocks were implemented, and we want to preserve the legacy behaviour there of not having a separate scope.
@jasmith-hs
Copy link
Contributor Author

Probably is good to do a minor release after this

@jasmith-hs jasmith-hs merged commit ce1d633 into master Mar 23, 2023
@jasmith-hs jasmith-hs deleted the do-block branch March 23, 2023 12:11
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.

1 participant