On 27 sep, 07:17, Malcolm Tredinnick <[EMAIL PROTECTED]> wrote:

Hi Malcolm.

> On Wed, 2008-09-24 at 02:36 -0700, bruno desthuilliers wrote:

(snip)

> I may not be completely understanding the problem you're explaining -- a
> short example from you may help illustrate the exact difficulty.

First point : my description of the problem was not only a bit unclear
(sorry), but also somewhat innacurate. I failed to take enough time
"reducing" the problem to minimal code - something I happily ask
others to do, my bad :(

After a couple more tests and investigations, it appears that the
nested blocks have nothing to do with my problem. What really happens
is that, given

- a context updating tag
- a base template base.html with a block defined in it,
- and another template another.html extending base and filling the
block

if you call the context updating tag at the "top level" of
another.html, the updated context is not accessible from within the
block.

I thought I had done this before without problem, hence my too-quick
conclusion that it had something to do with nested blocks.

> However, it looks like what you're seeing is expected behaviour:
> whenever a block ends (when an "endblock" marker is encountered),
> anything set inside that block in the context is popped off the stack
> and removed.

Yeps, context is a stacked mapping, so far so good (FWIW, I did reuse
Django's Context class in another app some three years ago...).

What I'm trying to do is to setup extra context in the 'extending'
template *before* filling in any block, and access this context from
within the block(s).

> Again, if I haven't understood what you are describing, please write a
> short example of two templates demonstrating what you are trying to
> achieve.

Here's a revised example, not even using nested blocks. 'set_context'
is a dumb context-updating tag written for the test, I didn't include
its code since it's mostly irrelevant here.

# flat_base.html
<html>
  <head><title>{{ yadda }}</title></head>
  <body>
    <h1>Flat Base Top</h1>

    <div id="flat">
      {% block flat %}
      <h2>Block flat</h2>
      {% endblock flat %}
    </div>
  </body>
</html>

# flat.html
{% extends "flat_base.html" %}
{% load my_tags %}

{% set_context "42" as answer %}

{% block flat %}
  {{ block.super }}
  Answer : {{ answer|default:"None" }}<br />
  {% set_context "42" as answer %}
  Answer : {{ answer|default:"None" }}<br />

{% endblock flat %}

So, the real question is : how is the first call to set_context
handled ? Is it expected that it doesn't update the context for the
whole template ?

FWIW, the use case is that I have some project-specific stuffs to
display in some templates that are 'served' by project-independant
apps - so I just can't set it up in the views - and I don't want to
use a context_processor since what I have to inject in the templates
1/ doesn't concern all templates and 2/ in each case requires some
really template-specific arguments. Also, having all this in a
context_processor would badly affect readability (IMHO) and prevent
our designers/integrators to get some control over it.  It's really
pure presentation logic, and as such should live in the concerned
templates.

There's of course some possible workarounds, mostly
1/ to call the template tag specifically for each concerned block, but
this imply x times more processing / querying / etc, which I'd rather
avoid since the involved processing is not totally trivial, and
2/ to add an extra template-inheritance level, which will also impact
performances, and also readability.
3/ to violate the DRY principle and copy-paste a good part of the base
template into the concerned ones.

I don't consider 2 and 3 as viable options, but I'd really really
prefer to avoid falling back on 1 if possible ('possible' including
patching the template system if necessary).

As a side note, and mostly out of curiousity : is it otherwise ok to
nest blocks, ie:

# nested.html
<html>
  <head><title>yadda</title></head>
  <body>
    <h1>Top</h1>
    <div id="outer">

      {% block outer %}
      <h2>Block outer</h2>

      <div id="inner">
        {% block inner %}
        <h3>Block inner</h3>
        {% endblock inner %}
      </div>
      {% endblock outer %}
    </div>

  </body>
</html>

This seems to work, but I didn't find any mention of it in the
FineManual(tm)...

Sorry once again for the first unclear and inaccurate post, and thanks
anyway for the answer and the good work in general.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to