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

Overriding Variables of Outer Scopes #44

Closed
MrOrz opened this issue Jul 6, 2011 · 14 comments
Closed

Overriding Variables of Outer Scopes #44

MrOrz opened this issue Jul 6, 2011 · 14 comments

Comments

@MrOrz
Copy link

MrOrz commented Jul 6, 2011

In Jinja 1.2 there was a feature that allows us to override outer scope variables from within a inner scope, by adding an exclamation mark after the set statement.

This feature is somehow missing in Jinja2. However, such feature is often required. Here is a real-world example in stackoverflow:
http://stackoverflow.com/questions/4870346/can-a-jinja-variables-scope-extend-beyond-in-an-inner-block

It would be glad to see such feature back again in Jinja2 ; )

@mitsuhiko
Copy link
Contributor

I will keep it in mind for the new compiler backend, but in the current one that is not possible to achieve for performance reasons.

@medwards
Copy link

I'd like to see this too.

@palmkevin
Copy link

+1

@smatthews1999
Copy link

I would like to see this too.

I'm trying to perform simple running totals in some generated data..and that functionality is what I need.

@smeggingsmegger
Copy link

+1

1 similar comment
@alexcasalboni
Copy link

+1

@ruiposse
Copy link

+1

I also think that this behaviour should be unacceptable:

    {% set first_run = True %} {% for i in range(3) %}
      {% for j in range(3) %} {% if first_run %} {% set first_run = False %}
    • {{ i }}, {{ j }}
    • {% endif %} {% endfor %}
    {% endfor %}

This will output 3

  • tags, but it should output only one.
    The variable resets after each outer loop run, even though it was declared OUTSIDE of all loops.

    I can't see how this would be expected behaviour. Is this a bug ?

  • @ruiposse
    Copy link

    +1

    I also think that this behaviour should be unacceptable:

    <ul>
      {% set first_run = True %}
      {% for i in range(3) %}
      <ul>
        {% for j in range(3) %}
        {% if first_run %}
        {% set first_run = False %}
        <li>{{ i }}, {{ j }}</li>
        {% endif %}
        {% endfor %}
      </ul>
      {% endfor %}
    </ul>
    

    This will output 3 <li> tags, but it should output only one.

    The variable resets after each outer loop run, even though it was declared OUTSIDE of all loops.
    I can't see how this would be expected behaviour. Is this a bug ?

    @skuda
    Copy link

    skuda commented May 15, 2014

    +1

    @thaddeusmt
    Copy link

    This would very useful when templating JSON files. +1

    @dpgaspar
    Copy link

    dpgaspar commented Jun 5, 2015

    +1

    @dlenski
    Copy link

    dlenski commented Jul 25, 2015

    Me too, could really use an override and it'd be great if the template designer docs explicitly mentioned this behavior!

    I had to work with some data in an (admittedly horrible) format and was puzzled at why the followed pre-pass didn't work...

    {% set last_one = none %}
    {% for row in rows %}
        {% set last_one = row.field %}
    {% endfor %}
    

    I agree with @ruiposse's comment that having a variable first set outside all loops reset itself inside them is very unexpected.

    @t2y
    Copy link

    t2y commented Nov 18, 2015

    +1

    @mitsuhiko
    Copy link
    Contributor

    I'm going to close this. This cannot work in all cases in Jinja2 and I do not really wish on supporting it.

    emacsway added a commit to emacsway/emacsway that referenced this issue May 5, 2020
    @github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2020
    Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
    Labels
    None yet
    Projects
    None yet
    Development

    No branches or pull requests