views:

29

answers:

3

The template inheritance page on the django site doesn't really solve my problem (Django 1.2).

My base page looks like:

...
<div class="grid_12" id="content">
   {% block content %}{% endblock %}
</div>
...
{% block javascript %}{% endblock %}

I have another template that defines content for these:

{% block content %}
   animated sidebar
{% endblock %}
...
{% block javascript %}
   alert('hello');
{% endblock %}

This is something like an animated sidebar, so I don't want to extend the base template since it's auxiliary to the main content of the page. If I just use "include", the entire thing is put where the "include" tag is placed - as a result the javascript doesn't run because it's included before one of its dependencies.

What's the best way to solve this?

EDIT

Sorry, I didn't make myself clear.

I have my content pages which render a template that extends "base.html". In "base.html" I want to include a sidebar template that needs to append blocks in "base.html". So I've tried just putting include "sidebar.html" into "base.html", but it just inserts the whole thing where the "include" tag is. What I want it to do is append the blocks in "base.html", which may themselves have been populated by "page.html".

Maybe it's important to say that "sidebar.html" is entirely static - i.e. there's no callable associated with it. So perhaps this question should really be "How can I include a static template into base.html so it will append to blocks in base.html regardless of the output of the actual view that processes the request?"

A: 

I think you mean you want to append to a block? You can put {{ block.super }} where you want the inherited content to go. e.g.:

{% block javascript %}
    {{ block.super }}
    alert('hello');
{% endblock %}
Matt Williamson
yes i do, but i also want this template included where i place it in the base template. it seems i can only append blocks if i'm using "extends", and then I enter an infinite loop.
Roger
Then you'll have to go the fun route and make custom template tags: http://docs.djangoproject.com/en/1.0/howto/custom-template-tags/
Matt Williamson
A: 

You should only use {% block foo %} tags to extend blocks in a base template, so I'm not clear what you mean when you say you don't want to extend it.

The code, as you've entered it, should render to

...
<div class="grid_12" id="content">
   animated sidebar
</div>
...
alert(hello)

Unless you want to append the content (as in Matt's answer) it's not clear what you want to happen.

Jordan Reiter
i've expanded my question for clarity.
Roger
A: 

You shoud be using something like jQuery to trigger execution only after the page is fully loaded. Include jQuery library in the document header and then somewhere:

$(document).ready(function() {
   //your code goes here
});
Paulo Scardine
the problem is my js file is included before jquery because it doesn't append to my javascript block, but just inserts the whole template into "base.html" where the "include" command is.
Roger