Nested blocks in Django templates - django

The master template in my Django app looks like this:
{% block parent %}
Some text...
{% block child %}
Default content here...
{% endblock child %}
...some more text
{% endblock parent %}
Now, this template should be overwritten in a way that the child block is changed:
{% extends "master.html" %}
{% block child %}
New content here...
{% endblock child%}
However, the rendering stays the same (printing "default content here..."). Have I missed something obvious or are nested blocks not possible? (Or, violating the DRY principle, have I to re-define the parent block?)
Edit: I'm working with Django 1.1, if that matters.

OK, it's a bug in Django's template system. For most other cases, Ben James is right (see his comment to my question above).
In my case, the child block was inside a {% ifnotequal a b %} block, and that breaks the block inheritance. I consider that to be a bug, since there are dozens of natural use cases for such operations.
The corresponding ticket.

Do this:
{% extends "master.html" %}
{% block parent %}
{% block child %}
New content here...
{% endblock child%}
{% endblock parent %}

Related

django templates: {% with %} variables blank inside of {% block %} within the {% with %}

edit: I've rewritten the test case to be absolutely minimal:
file 'test.html':
{% extends "testbase.html" %}
{% with greeting="Hi" %}
{% block content %} {{ greeting }} {% endblock %}
{% endwith %}
file 'testbase.html':
<html>
<body>
{% block content %} {% endblock %}
</body>
</html>
This produces the following HTML output when the test.html is rendered:
<html>
<body>
</body>
</html>
When the nesting of the with and the block content are reversed so that the with is inside, the "Hi" is rendered. Is there a problem with my templates? What sort of problem could cause this? How can one go about diagnosing this? I'm running Django 1.6.1 on python Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win32
It looks like it's not supposed to work like that, from a django-users question exactly like mine (below). Basically, it appears that the child template block contents are evaluated in the context of the parent template. If I put the with statement in the parent template, and the {{greeting}} in the child block contents, the "Hi" appears.
I don't think what you're describing has anything to do with the {%
with %} tag specifically. You're dealing with a child template here
-- one that starts with an {% extends %} tag. Nothing placed outside of {% block %} tags in a child template has any effect on the rendered
template. A child template simply supplies block contents to override
what has been specified in the parent template, so everything in a
child template must be included inside {% block %} tags. (If it is not
specified inside a {% block %}, where should it go in the parent
template?) See:
http://docs.djangoproject.com/en/dev/topics/templates/#id1

How to create a custom Django template tag working outside of a block?

In my project, I need to treat CSS requirements differently depending on several parameters. Thus, I need an additional level of abstraction compared to available static management systems.
In my templates, I would like to be able to register CSS (and JS) requirements with a template tag which would look like {% register_css 'myfile.css' %}. This approach works like a charm.
However, there is still one problem with this approach : the tag works perfectly when inserted in a block, but never gets called when inserted on top of a child template. The tag definition is:
#register.simple_tag(takes_context = True)
def register_css(context, *args):
context['static_registry'].register_css(streamlet)
return ''
This is my test template:
{% extends main_skelton %}
{% load static_registry %}
{% register_css 'base' %}
{% block title %}Welcome{% endblock %}
{% block content %}
{% register_css 'home' %}
<p>Some content here</p>
{% endblock %}
In that template, the 'home' css gets registered, but not the 'base' one. How may I make both work?
Thanks!
Include an empty {% block init %} in your base template, then override that block in the child templates to register files - {% block init %}{{ block.super }} {% register_css 'foo %}{% endblock %}
The underlying issue is that in a child template, only blocks that match something in the parent template will get rendered. AFAIK there's no easy way around that, but using a dummy block works just fine.

Django: How to include a block site wide?

Apologies, this is pretty basic. I have abstracted my static html navigation bar to a block so that it can be dynamically rendered from a model. I have created a new block marker in base.html with the following syntax
{% block navigation %}{% endblock %}
How can I ensure this is rendered on every page? Do I need to create some sort of middle layer for this? Everything I have done so far has simply used the primary block.
EDIT Sunday, 14 August 2011 11:25 AM
I didn't explain this very well. The content of navigation block is
{% extends 'base.html' %}
{% block navigation %}
<nav>
<ul>
{% for item in items %}
<li>{{ item.name }}</li>
{% endfor %}
</ul>
</nav>
{% endblock %}
I want to render this on every page without having to go through child templates of base.html individually and add it to them, if that is possible.
You have got static navigation bar, so you can just write code in your base.html page and then use it with {% extends 'base.html' %} tag on every new page. Your markup will be in every page. Also, if you use only extends tag in your child page and then render it - you will see base.html without any edition.
If you have got code, which need to appear on few pages, but not on everyone - create 'includes' directory, save code there and extend your base template with {% include %} tag. It won't avoid repating, but make your code shorter.
And last thing you'll need in future, maybe with dynamic code - caching. With {% cache %} tag you can cache block for some time.
Your block is not doing anything here, but if you want something to appear in all the templates, you have to define it in a base template (base.html) and make the others extend from it:
base.html
Yor menu and the stuff you want to appear everywhere
{% block content %}{% endblock %}
Another template
{% extends "base.html" %}
{% block content %}
The actual content of the page
{% endblock %}
{% block FOO %}{% endblock %} reserves a space to be overwritten in sub templates.
{% include "foo.html" %} pulls content from another file into the current file.
To get {% block navigation %}{% endblock %} to display on every page, you need to add content
{% block navigation %}SHOW ME{% endblock %}

Override inner template block in Django

I have the following structure of templates (simplified for clarity):
base1.html:
<html>
<head>{% block head %}{% endblock %}</head>
<body>{% block body %}{% endblock %}</body>
</html>
base2.html:
{% extends "base1.html" %}
{% block head %}
<meta .... />
<title .... />
css includes etc.
{% endblock %}
{% block body %}
{% block header %}{% endblock %}
{% block featured %}{% endblock %}
{% block navigation %}{% endblock %}
{% block content %}{% endblock %}
{% block footer %}{% endblock %}
{% endblock %}
Also base3.html and base4.html which futher detalize the generic body structure defined in previous base templates (not shown here). The last template extends base4.html, overriding blocks with concrete content (markup is handled by base templates).
The question is: I have two templates: main.html and article.html which both extend base4.html. But in article.html I want the order of body blocks defined in base2.html to be different (featured block to go after navigation). How can I achieve that? Or how to refactor the structure of templates to make this possible? Overriding the block body in top-level template doesn't work.
I don't think that you're going to have any luck going about it that way. A simple solution could be to have an optional variable that base2.html looks at, which determines the alternate order. In fact it may even be that you can define the presence of this variable in the article.html template itself. I haven't tried this, but something like the following may work:
{% with alternate_order=1 %}
{% include base4.html
{% endwith %}
Why do you have so many levels of inheritance? As a note, django provides no limitations, but typically we use the three-level approach. See docs
Here are a few suggestions:
You can probably combine base1.html and base2.html.
If you can put the html code in the parent then do so. I usually implement my templates like this:
<div id='sidebar'>{% block sidebar_content %}{% endblock %}</div>
As for your problem, if you need a different layout, that's when you branch. That is, if using a different css won't work.

Is there a way to make a block optional in Django template

In Django's templates system, if I have a block that I want to make optional using an if statement, how do I do it?
I was trying this:
{% if val %}{% block title %}Archive {{ foo }}{% endblock %}{% endif %}
But that doesn't work. Is there a way to do that, so that for a given value (in this case Null) the block isn't issued and the base template uses the original values?
Edit:
Let me be a little more specific, so that it is easier to answer.
I have a page with 10 entries per page. The user can then go on to the next page and see the next ten items. For each further page they go, past the first, I would like to have the title tag say something like "Archive 1" or "Archive 10" but if they get back to the original page, it is no longer archive, and it should just go to the original site title already given in the base templates.
I ran into a similar issue with a project I'm working on. Here's how I resolved it using {{ block.super }} to pull the default value from the parent block:
My parent template contains:
{% block title %}Default Title{% endblock %}
My child template contains:
{% block title %}
{% if new_title %}{{ new_title }}{% else %}{{ block.super }}{% endif %}
{% endblock %}
*Note: You may want to wrap the code in {% spaceless %}{% endspaceless %} if you plan to use the result in an HTML title tag.
(It looks like Jordan Reiter posted the same solution in the comments of the original question a bit before my response.)
As far as I understand blocks are placeholders to be "overridden" in the child templates. They have to be defined at "compile time" and not at "run time".
As for your specific problem why not modify the title based on the page number (assuming you use pagination)? Something like this:
{% block title %}
{% ifequal page 1 %}Current{% else %}Archive {{ page }}{% endifequal %}
{% endblock %}
I would only have to add to the good answers above that depending on the Django version sometimes the {{ block.super }} puts the content from the master twice, this seems to happen in the most recent versions of Django.
I am using Django 1.8 and whenever i put the {{ block.super }} it started to behave in that way just as an addition to the Jamie answer i can say that in the base template you can put the content you wish
{% block title %} Default Title {% endblock %}
And then in the child if you want the footer to be inherited and displayed just do not do anything it will be. But if you do not want that block to be displayed then put the tag in the child with empty content just like this:
{% block title %}
{% endblock %}
Then it will be hidden once it is rendered also you can overwrite the content on it if you wish.