Skip to content

Auto global variables? #237

Closed
Closed

Description

@gforcada

The following snippet of Chameleon template does not fail, as one would expect on the last <p> with a unknown local_value error, but rather shows 35 the first value assigned on local_variable.

<p tal:define="local_value python: 35;
               local_value python: 33">
  ${local_value}
</p>
<p>OUTSIDE ${local_value}</p>

From what we (me and @staeff) found, it seems that the problem is reassigning a value on the same tal:define block, if you do it nested then, Chameleon correctly complains about the 2nd paragraph.

Is that something expected?

Redefining the same variable on the same tal:define seems weird, but we use it in a case where we want to get a different value (an image scale) based on a condition, so:

<p tal:define="image_tag python: len_articles == 1 and get_big_scale or nothing;
               image_tag python: len_articles == 2 and get_small_scale or image_tag;">
DO SOMETHING 
</p>

With this we try to do: if there is only one article the a big image scale, but if there are two the the smaller one. But as all define statements are evaluated, the second statement runs always and thus if the article count is different than two, we want to keep the value set on the first statement.

Activity

  1. malthe commented on Mar 30, 2017

    @malthe
    Owner

    That's interesting. I don't understand how it can be so since defines are scoped in a very simple way.

    If you look at the source code, perhaps you can figure out what's happening?

    To do this, set the environment variable CHAMELEON_DEBUG=1 and either have the template source written to disk or simply place the following instruction somewhere in the template:

    <?python import pdb; pdb.set_trace() ?>

    This should allow you to look at the source code and poke around.

  2. gforcada commented on Mar 30, 2017

    @gforcada
    ContributorAuthor

    @malthe thanks for the quick answer, I added a pdb on the template, then:

    export CHAMELEON_CACHE=/tmp/chameleon
    ./bin/instance fg
    

    And looking at the code, it's hard for me to say where is actually the problem :-/ On the meantime I put the conditional out of the same define and things are working...

  3. dataflake commented on Feb 18, 2021

    @dataflake
    Collaborator

    I have stepped through the cached Python code and found the issue.

    Every time a variable name is defined with tal:define the code looks up the variable name in the expression context first and saves the existing value (or a marker that means "the variable did not exist before") into a temporary variable. This is a simple way of checking if a variable name has been set in a higher context, its value is supposed to be saved and then restored when leaving the current context.

    When the code for the tag with the define is finishing the variable name is removed from the expression context - but only if the backed-up value indicates that the variable did not exist before. Otherwise the backup value is put back into the expression context.

    In the case where a variable with the same name is defined more than once the process fails. The second definition of the variable will back up the value from the first definition. At cleanup time the backed up value is found and put back into the expression context

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions