Control block visibility in a subtemplate

535 views
Skip to first unread message

Berco Beute

unread,
May 26, 2008, 11:23:27 AM5/26/08
to Django users
In a subtemplate I want to control which of the blocks specified in
the parent template are visible. I know I can hide a block by
overriding it with an empty block, but instead of overriding all the
blocks I DO NOT want to show I want to specify the blocks that I DO
want to show. Is that possible at all?

2B

Bruno Tikami

unread,
May 26, 2008, 12:11:27 PM5/26/08
to django...@googlegroups.com
2B,

You can specify empty blocks on the parent template and "load" (overwrite) the blocks you want inside IF blocks. Does it solve your problem ?

Regards,

Tkm
--
[]s!

Tkm
http://djangopeople.net/brunotikami/

Berco Beute

unread,
May 26, 2008, 4:23:50 PM5/26/08
to Django users
Hi TKm,

Thanks, but that won't work since the blocks in the parent are not
empty. What I want that subtemplates specify which blocks they use
without specifying the content of each block. The content of the block
is retrieved from the parent template using 'block.super', as in:

{% block title %} {{ block.super }} {% endblock %}

2B


On May 26, 6:11 pm, "Bruno Tikami" <brunotik...@gmail.com> wrote:
> 2B,
>
> You can specify empty blocks on the parent template and "load" (overwrite)
> the blocks you want inside IF blocks. Does it solve your problem ?
>
> Regards,
>
> Tkm
>

Berco Beute

unread,
May 27, 2008, 3:42:45 AM5/27/08
to Django users
Hmm...turns out that my approach is not going to work at all. In the
subtemplate I can only use this once:

{% block title %} {{ block.super }} {% endblock %}

So in the subtemplate there is no way for me to use this title block
in two places. That defeats the purpose of having a subtemplate that
only specifies which parent blocks should be shown.

Is there another way I can accomplish this that I may be overlooking?

2B
Message has been deleted

Berco Beute

unread,
May 27, 2008, 4:04:23 AM5/27/08
to Django users
Maybe I should describe a little better what I'm trying to accomplish.

I want to create some sort of skinning framework where skinners can
create subtemplates (a.k.a. skins) that only specify which blocks from
the parent template should be visible. The parent template specifies
all available blocks and fills them with the appropriate content. I
don't waht to bother the skinners with the task of retrieving the
content from the backend. That way skinners only have to care about
which and how blocks show up in their subtemplate. It would be
possible to put each block in an HTML DIV and set the visibility of
each block at the subtemplate level, but that would mean that each
subtemplate has all the available blocks. Some are hidden for sure,
but that's far from elegant.

I hope that makes it a little clearer.

2B

Bruno Tikami

unread,
May 27, 2008, 9:38:11 AM5/27/08
to django...@googlegroups.com
Well, I'm not sure if I understood exactly what you need but I don't think you can do this without overwriting blocks.

What I suggested was to define all the blocks on a parent template but don't fill them with any content, like django's admin base.html and base_site.html do.

If you need anything else, add me on gtalk: br...@tikami.com.br

Tkm

[code]

#base.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="{{ LANGUAGE_CODE }}" xml:lang="{{ LANGUAGE_CODE }}" {% if LANGUAGE_BIDI %}dir="rtl"{% endif %}>
<head>
<title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" type="text/css" href="{% block stylesheet %}{% load adminmedia %}{% admin_media_prefix %}css/base.css{% endblock %}" />
{% if LANGUAGE_BIDI %}<link rel="stylesheet" type="text/css" href="{% block stylesheet_rtl %}{% admin_media_prefix %}css/rtl.css{% endblock %}" />{% endif %}
{% block extrastyle %}{% endblock %}
{% block extrahead %}{% endblock %}
{% block blockbots %}<meta name="robots" content="NONE,NOARCHIVE" />{% endblock %}
</head>
{% load i18n %}

<body class="{% if is_popup %}popup {% endif %}{% block bodyclass %}{% endblock %}">

<!-- Container -->
<div id="container">

    {% if not is_popup %}
    <!-- Header -->
    <div id="header">
        <div id="branding">
        {% block branding %}{% endblock %}
        </div>
        {% if user.is_authenticated and user.is_staff %}
        <div id="user-tools">
        {% trans 'Welcome,' %} <strong>{% if user.first_name %}{{ user.first_name|escape }}{% else %}{{ user.username }}{% endif %}</strong>.
        {% block userlinks %}
        <a href="{% url django.contrib.admin.views.doc.doc_index %}">{% trans 'Documentation' %}</a>
        / <a href="{% url django.contrib.auth.views.password_change %}">{% trans 'Change password' %}</a>
        / <a href="{% url django.contrib.auth.views.logout %}">{% trans 'Log out' %}</a>
        {% endblock %}
        </div>
        {% endif %}
        {% block nav-global %}{% endblock %}
    </div>
    <!-- END Header -->
    {% block breadcrumbs %}<div class="breadcrumbs"><a href="/">{% trans 'Home' %}</a>{% if title %} &rsaquo; {{ title|escape }}{% endif %}</div>{% endblock %}
    {% endif %}

        {% if messages %}
        <ul class="messagelist">{% for message in messages %}<li>{{ message|escape }}</li>{% endfor %}</ul>
        {% endif %}

    <!-- Content -->
    <div id="content" class="{% block coltype %}colM{% endblock %}">
        {% block pretitle %}{% endblock %}
        {% block content_title %}{% if title %}<h1>{{ title|escape }}</h1>{% endif %}{% endblock %}
        {% block content %}
        {% block object-tools %}{% endblock %}
        {{ content }}
        {% endblock %}
        {% block sidebar %}{% endblock %}
        <br class="clear" />
    </div>
    <!-- END Content -->

    {% block footer %}<div id="footer"></div>{% endblock %}
</div>
<!-- END Container -->

</body>
</html>


#  base_site.html

{% extends "admin/base.html" %}
{% load i18n %}

{% block title %}{{ title }} | {% trans 'Django site admin' %}{% endblock %}

{% block branding %}
<h1 id="site-name">{% trans 'Django administration' %}</h1>
{% endblock %}

{% block nav-global %}{% endblock %}

[/code]

Berco Beute

unread,
May 27, 2008, 9:52:07 AM5/27/08
to Django users
It certainly is possible to fill the blocks in the parent and show
that content in the subtemplate using:

###PARENT###
{% block title %} blablabla {% endblock %}

###SUBTEMPLATE###
{% block title %} {{ block.super }} {% endblock %}

There are 2 problems with this approach:

1. I can only use a block once in my subtemplate.
2. All blocks defined in the parent will show up unless I explicitly
override a block witn an empty block.

Thanks for your help so far!
2B

Martin Glueck

unread,
May 27, 2008, 9:58:41 AM5/27/08
to django...@googlegroups.com
How about using the include tag:
  • Define the content of your "blocks" in a seperate template file
  • Include these "block" files in your "skin" template
This will allow you that you can use one "block" more than once, and you only need to include the "blocks" you would like to see in the final page.

Martin
Message has been deleted

Berco Beute

unread,
May 27, 2008, 12:07:58 PM5/27/08
to Django users
Excellent! Thank you very much.

2B

On May 27, 3:58 pm, "Martin Glueck" <martin.glu...@gmail.com> wrote:
> How about using the include tag:
>
>    - Define the content of your "blocks" in a seperate template file
>    - Include these "block" files in your "skin" template
>
> This will allow you that you can use one "block" more than once, and you
> only need to include the "blocks" you would like to see in the final page.
>
> Martin
>
Reply all
Reply to author
Forward
0 new messages