views:

108

answers:

2

I just want to define id attribute of body tag in child template. First solution works perfectly:

base.html:

[body{% block bodyid %}{% endblock %}]

child.html:

{% block bodyid %} id="myId"{% endblock %}

It's simple and cool! But I don't like to point id="myId" in every child template. I want just send value 'myId' to parent template, where it put to id="....". So, I invent this method:

base.html:

[body{% block bodyid %} id={{ bodyid }}{% endblock %}]

child.html:

{% block bodyid %}
    {% with 'myId' as bodyid %}
        {{ block.super }}
    {% endwith %}
{% endblock %}

But it's terrible and tedious to compare first solution. Is there any good method to do this?

This problem is deeper, than managing bodyId. I think, I try to find and organize subtemplate system through standard django template's inheritance.

+2  A: 

In the base template:

<body id="{% block bodyid %}{% endblock %}">

In the child template:

{% block bodyid %}myId{% endblock %}

If I understand your question correctly, this should be sufficient to achieve what you want; only send the actualy ID (myId) to the base template.

Ayman Hourieh
this is the method i use all the time
Rasiel
Year, but I don't want leave empty id="", when I don't define {{ bodyId }} in child.html
ramusus
A: 

You, if you prefer, set this via your code - so that your id's are part of your context object, and therefor are not required to be entered in the child templates, just the views that call them.

You can then tell your base template to get the id from the context object, or rather just use the template syntax for it:

<body id="{{ body_id }}">

Personal preference here of course, and it depends on the structure of your templates and views, but its the way I'd do it.

Mike Scott
Year, but I don't want leave empty id="", when I don't define {{ body_id }} in my view or child.html
ramusus