-
Notifications
You must be signed in to change notification settings - Fork 15
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
Blocks defined in partial included in base template, fails to be overriden in extended templates #272
Comments
After running |
The official jinja implementation returns for what you provided:
I ran it with this (python) code: import jinja2
templateLoader = jinja2.FileSystemLoader(searchpath="./templates")
templateEnv = jinja2.Environment(loader=templateLoader)
template = templateEnv.get_template("base.txt")
outputText = template.render()
print(outputText) So |
Reported by Ciprian Dorin Craciun ([@cipriancraciun]) in issue [rinja-rs#272]. [rinja-rs#272]: <rinja-rs#272> [@cipriancraciun]: <https://github.com/cipriancraciun>
I can confirm @cipriancraciun's finding. Please see #278. |
Assigning myself for the fix as I have a good idea on how to fix it. |
Wait no. Don't know how but I failed to correctly set the python test... So no, the current output of
The reason is because |
I opened #279 which adds some extra explanations about this. |
Give that the conclusion of this issue is "work as expected", then I think I don't understand the exact semantics of For me, the following two snippets should be identical with regard to their output:
Thus, in both cases, when the extended extends both blocks, the output should in both cases write Else, if one can't override blocks defined in partials, then why allow them to be defined? (From my understanding of Jinja / Rinja / Askama, the main and only usage of blocks is to aid overriding in extended templates.) Setting this asside, is there another way to "include verbatim" a template source in another template? |
The important thing here is which |
Could you expand a bit on this please? (I didn't understand what you were trying to convey here.) Based on my interpretation of the above, I've tried to move the What is even weirder, is that if I run your Python Jinja2 snippet, but running the
Now, I could understand why (Or even why |
Digging a bit into the Jinja2 / Rinja compatibility, I think -- if the target is Jinja2 compatibility -- that then the current Rinja code is indeed "working as expected", because the Jinja2 documentation states the following with regard to includes:
Thus in my example, because the partial defines a block, that would be scoped only to the partial itself and the base template it might happen to extend. However, then my other question stands: how does one actually include verbatim (as in C's |
Use Also don't forget that you can actually prevent to have jinja code generated with conditions. Like you can set a variables or a field and then check if this variable/field exist as condition for example, like:
Like this, the block will not be defined in the other templates. |
That's the issue, I can't use To clarify my use-case (which I don't believe is that unique):
|
Given the following example:
Namely:
base
andextended
template,base
template indirectly defines a blockblock_in_partial
, via the{% include "partial.txt" %}
,extended
although overrides the contents of theblock_in_partial
,extended
is rendered, the overriden contents fails to output;Observed outcome of rendering
extended
:Expected outcome of rendening
extended
:For some reason the parser / generator fails to register the
block_in_partial
(included inbase
) as an extendable block.Moreover, the current Rinja documentation supports the case, by implying that
{% include ... %}
statements should behave as if they wouldn't be there, and the contents of the included template would be spliced at the place of inclusion:What are potential use-cases:
base
templates (for example one for mobile, one for HTML-only, one for modern browses, etc.) which reuse some common sections that are extracted in partials; but the partials need some placeholders to include content controlled by extending templates;The text was updated successfully, but these errors were encountered: