Re: Template Inheritance doubt
It sounds like you are trying to have a template inherit code from multiple parents. One option is to stay with a simple inheritance hierarchy where each child template extends at most one parent template. If you want to use different code for different sub-sections of your project then just add a level to the hierarchy.
For example, you could add the content of topbar.html and sidebar.html into the topbar and sidebar blocks in the base template (i.e. inside your current topbar and sidebar block tags in base.html). If a view template then extends the base template, but you do not want to include the topbar and sidebar code, add empty topbar and sidebar blocks to the view template so that the base code gets swapped out with an empty block.
If you have different sections of your app and want to use different topbar and sidebar code in each, then add a level to your template hierarchy - something like:
base.html - overall site template
s1_base.html - extends base.html - put section specific topbar and sidebar code here
view1.html - extends s1_base.html - put view specific stuff in here
In this case you could leave empty topbar and sidebar blocks in base.html, adding the content to s1_base.html instead. Or you could even have have view1.html extend sidebar1.html, which extends topbar1.html, which extends base.html or something like that (although I usually try to limit the hierarchy to 3-4 levels to make debugging easier).
Hope that helps.
R,
Dow
On May 18, 2013, at 2:14 AM, Parin Porecha <parinporecha@gmail.com> wrote:
> Hi,
>
> I have just started to learn Django's template inheritance and I am stuck due to this problem -
> In my application's 'base.html', I have defined two blocks -
> {% block topbar %}{% endblock topbar %}
> and
> {% block sidebar %}{% endblock sidebar %}
>
> I have 2 templates - 'sidebar.html' which extends {% block sidebar %}
> and 'topbar.html' which extends {% block topbar %}
>
> The 'sidebar.html' is also divided into further blocks which are extended by other templates.
>
> Now, my question is -
> in 'views.py', i have a view which is supposed to render the whole homepage ( i.e.- including the sidebar, topbar and content ). So, which template should I load in the view, so that both sidebar and topbar plus the content defined in other templates gets shown ?
> because 'base.html' contains many children, I'm confused which one to load
>
> Thanks to mattmc who tried to help me with this in IRC. Sorry, didn't get it completely :)
>
> --
> Regards,
> Parin Porecha
>
> say Kifflom! and look forward to 17.09.13 !
>
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home