pallets / jinja

A very fast and expressive template engine.
https://jinja.palletsprojects.com
BSD 3-Clause "New" or "Revised" License
10.23k stars 1.6k forks source link

Fix: Autoescape does not work well across blocks/inheritance #1909

Open riyaz489 opened 10 months ago

riyaz489 commented 10 months ago

Issue: As compiled Jinja Blocks does not have info about parent template scopes like autoescape, due to which it is ignoring those inherited properties.

Fix:
Fixed the compiler. We made blocks compilation volatile so that it will figure out the correct property during rendering time.

Issue: Ideally, the child template body should contain blocks as the first child. because only blocks in the child template will be replaced with the blocks in the main template. but whenever the developers write blocks inside other Scoped Nodes, the block is compiled as a part of scoped Node code by the Jinja compiler. Once the child template is compiled, then the Jinja compiler complies all blocks separately as well. Due to this behaviour blocks were compiled multiple times. once when they are part of a scoped node's body and again when all blocks are compiled. This behaviour leads to unexpected results

Fix: Fixed Node Parser. So each node in the child template body is inspected to identify the first block node and move it into the template body. (to avoid multiple parsing of blocks.) These blocks will be seamlessly placed in the main template body. Because outer scoped nodes of blocks do not make any scene for the child template as only data inside blocks will be replaced in the main template.

Checklist: