>
> I suspect it has to do with {% if %} being interpreted at runtime while 
> {% block %} is interpreted at compile time. 
>
> I never investigated this fully. If someone does, I’m interested :-) 
>
> -- 
> Aymeric. 
>

I took a look at how this works. The issue comes from a naive approach to 
how block nodes are selected to be overridden in the ExtendsNode. I wrote 
some pseudocode below that I think clarifies what's happening:

Compile step:

parser.parse() is called
-  if got extends node
  - call do_extends
    - call parser.parse() to get rest of nodelist for current template *
    - ExtendsNode.__init__
      - get block nodes with nodelist.get_nodes_by_type(BlockNode) **
    - ExtendsNode.render
      - add BlockContext instance to context ***
      - add block nodes from __init__ call to BlockContext
      - compile parent
      - add parent blocks to BlockContext **
      - call parent._render(context)

* At this point, it's possible nodelist has IfNodes that contain BlockNodes.
** nodelist.get_nodes_by_type(BlockNode) returns all block nodes, ignoring 
any conditionals
*** BlockContext is a defaultdict of block name -> block list, where 
block_context[name][-1] is the winning block.

Render step:

When BlockNodes are rendered they check for a BlockContext in the context. 
If an entry exists in BlockContext with the same name, it is rendered in 
place of the current block. Because block_context[name][-1] is the one that 
wins, and blocks are added to BlockContext regardless of the condition 
around it, the parent block is always overridden.

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/23735aaf-014c-4598-923c-df2ed7180e99%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to