-
Notifications
You must be signed in to change notification settings - Fork 641
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
Block scoping is too strict (stricter than Jinja) in 3.0.0. #906
Comments
* Previews are mostly self-styling * Oops * add split filter and use it to collate font weight and style * Font weights and styles are automated in previews * Start naming preview argumets * Organize macros * Add size/ratio previews (first draft) * parse named options for preview annotation * show 'regular' variant even though it's not a style or weight * Display font sizes with line heights * Release alpha for testing * macros and styles for @parameter and @require (WIP) * new require * require links correctly!!!! * display @link and @see annotations * fake test data * Remove excess example * Rename and partially document herman-accoutrement utilities * remove all caps color name * Better color and font previews * Start on improved size previews * ratios, sizes, and examples * Document changes * Upgrade deps. * Add toggle/tab js. * Add circle.yml * Style cleanup * Move preview/example styles into main sheet * Scale cleanup * Do not use ID attrs (which may contain special chars) as selectors. * Escape names for IDs * upgrade nunjucks * Add bottom-border to tables * Only display code-blocks if they have content * Add margin to lists * Add focus and punctuation to font previews * Downgrade nunjucks (mozilla/nunjucks#906).
We got the same problem here. Not being able to access local variables inside of {% call %} blocks seems to break a lot of standard use cases. I would strongly vote to rollback the strict scoping for {% call %} blocks. |
Introduced in 429f636 |
Seems to be related to my similar issue #912 |
This should be fixed but I'd love to see the PR for adding |
|
@ArmorDarks I get that, does that mean it's dead as a potential feature for Nunjucks? I think feature parity is important, could it be done as an extension perhaps? |
@paulmsmith Well, I'm not a maintainer, I can't tell :) Personally I'm not very happy with I think for now it wasn't merged only because new maintainers didn't have enough time to review PR. My guess. |
Hi guys! Any updates on this issue?.. |
I dug into this for awhile yesterday, but didn't get very far. Reverting the changes to I pushed an initial attempt at oddbird@104c46c in case that helps someone else. It fixes one of the test cases, but breaks some others. |
I'm starting to doubt that it's possible to properly support all those test cases. In JS we don't have such issue with scoping and leaking, because we can control when we want variable to "leak", and when not. For instance, this will cause intentional leak: let test = 1
let funcTest = () => { test = 2 }
funcTest()
console.log(test) // will yield 2 And this won't let test = 1
let funcTest = () => { let test = 2 } // let will create new scoped variable
funcTest()
console.log(test) // will yield 1 In Jinja and Nunjucks we have only Maybe instead of solving this puzzle we should lift this issue to Jinja2 community? Though, Python community doesn't seem to favor JS developers much, so I guess there are very little chances that they will reevaluate concept of Well, this is only partially related to this issue, but it might make things much simpler, since it will move responsibility for scoping in hand of developer instead of compiler. |
I think all of these test cases are already supported by Jinja. |
I can only guess, since I never worked with Jinja. Though, if Jinja works exactly the way all those newer test cases describes, this already means that in Jinja it's impossible to change global variable by macro. Not big loss, though, but still this seem like an oversight in design of the language. |
This issue seems to be related too #679 (comment) |
Since we have new maintainers (welcome and thanks, @fdintino!), I'd like to bump this bug again. It's a pretty major bug in v3.0.0, and I wasn't able to figure it out after digging around awhile in the code (#906 (comment)). Many thanks to anyone willing to give it a shot! |
I'll try to take a look at this issue over the next weekend. |
Wondering what the status of this issue is now? |
Same thing here... This bug makes Nunjucks 3.0.0 unusable... |
@noahlange any chance they your refactoring work addresses this bug? |
@fdintino - not at the moment, sorry. 😕 I'll do some digging and see if I can track this down. |
I have a fix for this. I'll clean it up later today and push it out. What threw me (and I'm guessing others as well) is that there are tests that test for the wrong behavior for |
@fdintino Nice to hear it! Can you point to that wrong test? Just curious. |
@ArmorDarks it was these two tests. The values returned from jinja2 are |
Allow {% call %} blocks to access parent scope. Fixes #906
@fdintino Hm, looking on those tests I'm starting to doubt that Jinja's behavior is correct. Shouldn't declaration of variable before scope make any changes in that scope propagate to declared in upper scope variable? In other words, current tests behavior seems to be correct. Is behavior same in Python? I don't have any experience with Python, so I can't check myself. |
It's a common misconception that Jinja2 follows the conventions of Python everywhere, but that's not generally true. It supports a subset of python syntax, but template-specific constructs like blocks, macros, includes, template scoping, and the I will note that Twig does not isolate writes inside |
Ah, now I see that Jinja2 specification about set scoping within |
There were a number of commits included in 3.0.0 that fixed scoping issues, but now it seems that
{% call %}
blocks no longer have access to variables set in their parent scope. This differs from the Jinja2 implementation.In Jinja2, the following code:
Outputs:
In Nunjucks 3.0.0, the output is:
See http://jsfiddle.net/jgerigmeyer/mjpbm13j/
The text was updated successfully, but these errors were encountered: