On May 28, 7:26 am, "Daniele Procida" <dani...@apple-juice.co.uk> wrote: > On Thu, May 28, 2009, Masklinn <maskl...@masklinn.net> wrote: > >> I need to create some templates that will cover a number of cases: > > >> display navigation menu: yes or no > >> display additional info box: yes or no > > >> In other words, there are a total of four cases that need to be > >> provided for. > > >> I understand how to use template inheritance so that a derived > >> template > >> will contain a block that overrides the default presence of the > >> navigation menu. > > >> I could create a total of four derived templates, one for each case; > >> that would be OK too. > > >> However, the number of derived templates rises exponentially. If I > >> needed to cover a couple of additional options, say: > > >> use low-bandwidth styling: yes or no > >> display secret information: yes or no > > >> then I'd need 16 templates to extend the base. > > >> There must be a better way to do this - what is it? > > >Template inclusion instead of template inheritance? > > You mean, by using a series of tags like: > > {% if [condition] %} > {% include "navigationmenu.html" %} > {% endif %} > > {% if [condition2] %} > {% include "additionalinfo.html" %} > {% endif %} > > and so on? >
Yes, that could work. Also, if you evaluate the conditions in the view, instead of the template, you can pass in the file to be included as a parameter. if condition1: context['extra_includes'] = 'navigationmenu.html' elif condition2: context['extra_includes'] = 'additionalinfo.html' This keeps your template nice and clean: {% include extra_includes %} Of course, the tradeoff is that you might view the conditions as having more context in the template. Here are the docs for include: http://docs.djangoproject.com/en/dev/ref/templates/builtins/#include --~--~---------~--~----~------------~-------~--~----~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---