You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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:
<ptal: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.
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.
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...
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
Activity
malthe commentedon Mar 30, 2017
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=1and either have the template source written to disk or simply place the following instruction somewhere in the template:This should allow you to look at the source code and poke around.
gforcada commentedon Mar 30, 2017
@malthe thanks for the quick answer, I added a pdb on the template, then:
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...
dataflake commentedon Feb 18, 2021
I have stepped through the cached Python code and found the issue.
Every time a variable name is defined with
tal:definethe 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
defineis 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