You can also make use of Inclusion Tags. Very easy to use:

http://www.djangoproject.com/documentation/templates_python/ 
#inclusion-tags

I use them for all dynamic sections that appear across templates.

On 22/06/2006, at 8:32 PM, Malcolm Tredinnick wrote:

>
> On Thu, 2006-06-22 at 10:01 +0000, Jaroslaw Zabiello wrote:
>> Django uses Python modules instead of classes. The problem is how to
>> avoid duplication of the code in controllers when I want to put some
>> data to parent templates? Let see the example:
>>
>> base.html:
>> {% block welcome %}
>> Hello {{ name }}!
>> {% endblock %}
>> {% block main %}{% endblock %}
>>
>> test1.html:
>> {% extends "base.html" %}
>> {% block main %}blah, blah{% endblock %}
>>
>> test2.html:
>> {% extends "base.html" %}
>> {% block main %}different blah, blah{% endblock %}
>>
>> How to deal with the same, shared blocks? Do I have to copy code for
>> all shared blocks? It looks very bad.
>>
>> views.py:
>> def test(request):
>>     return render_to_response('test.html', {'name':'Jarek'})
>> def test2(request):
>>     return render_to_response('test2.html', {'name':'Jarek'})
>>
>> I would like to have something like RoR, where all shared  
>> variables for
>> all shared (partials) templates can be set in one, and only one,  
>> place:
>
> If only some of you views need this default data, make a single  
> function
> that populates the default or standard data and returns a dictionary.
> Then your specific view functions can call that function initially and
> subsequently update the dictionary with their changes. That's pretty
> standard programming practice -- factor out the common bit (just  
> like in
> your Ruby example).
>
> If every single view needs the same piece of data added to the  
> context,
> write a template context processor (see [1]), such as how the auth
> middleware adds the "user" variable into the context.
>
> [1]
> http://www.djangoproject.com/documentation/templates_python/ 
> #subclassing-context-requestcontext
>
> Regards,
> Malcolm
>
>
>
> >


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

Reply via email to