Skip to content

Add support for Jinja's with tag - #1365

Open
Mohith26 wants to merge 1 commit into
HubSpot:masterfrom
Mohith26:feat/with-tag
Open

Mohith26 wants to merge 1 commit into
HubSpot:masterfrom
Mohith26:feat/with-tag

Conversation

@Mohith26

@Mohith26 Mohith26 commented Nov 4, 2026

Copy link
Copy Markdown

Closes #1121

Adds Jinja's {% with %}...{% endwith %} scoped assignment tag. Right now this fails with Unknown tag: with:

{% with jobname="my_job", schedule="0 1 * * 0", deadline=300 %}
  {% include 'objects/CronJob.jinja.yml' %}
{% endwith %}

Behavior follows Jinja:

  • names assigned by the tag, and anything set inside the block, are gone after endwith, and outer variables with the same name come back
  • every value is evaluated in the enclosing scope before any name is bound, so {% with a = 1, b = a %} gives b the outer a
  • {% with %} with no assignments just opens a scope

How it works: WithTag splits the assignments on top-level commas (commas and = inside strings, brackets and comparisons like ==/<= are left alone), enters a scope, runs a multi-target set ({% set a, b = x, y %}) through the registered set tag, then renders the body. Going through set means eager execution gets the existing deferred-assignment handling. EagerWithTag renders the block in a child context and, if anything stayed deferred, wraps the output with wrapInChildScope the same way EagerForTag does:

{% with a = deferred, b = 2 %}{{ a }} {{ b }}{% endwith %}
->
{% for __ignored__ in [0] %}{% set a, b = deferred, 2 %}{{ a }} {{ b }}{% endfor %}

If set is disabled in the config, a with that assigns adds a DISABLED error and renders nothing (a bare {% with %} still works), so it can't be used to get around a disabled set tag.

Tests:

  • WithTagTest: scoping, shadowing, outer-scope evaluation, nesting, loop variables, commas/operators inside values, the example from the issue, and syntax errors (bad target, missing =, missing value, duplicate names)
  • EagerWithTagTest: runs all of the above in eager mode, plus deferred cases that render the eager output a second time and check the final result
  • WithTagDisabledSetTest: default and eager mode

mvn verify passes locally (2272 tests, plus checkstyle/pmd/spotbugs/spotless). Note that the checked-in wrapper is Maven 3.6.2, but git-commit-id-maven-plugin 9 needs 3.6.3+, so I ran it with Maven 3.9.9.

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.

Support for "with" statement

1 participant