Django beginner. I have a base template with a logo, and three subsections for content. Two of the subsections contain html by default, and the remaining subsection is for the content of each child page.
I would now like to create a different page, with the exact same logo, but with different subsections / content. e.g. I may want just two subsections, formatted horizontally instead of vertically etc.
So, for this I guess I need to create a new template – the problem is I am violating the DRY principal by having the exact same logo html code in the new template as the first template.
So, are there any design patterns to resolve this issue of repeating the logo code in this case ? I was thinking about having variables like isPage1 or isPage2 passed to the template, and then I would enable/disable blocks based on this – is this a feasible method, and can anyone provide any alternatives ?
Many thanks
Yes, there is a pattern that exactly fits your needs. It’s called template inheritance in DJANGO.
You will basically have a base template with your header, logo, and main content placeholder. Something like (extracted from the link I placed above):
Then, on your actual web page (the one using the template) you will have:
Note that the base template here is called
base.html. So, in your web page you extendbase.htmlby placing{% extends "base.html" %}. Then, in that page, you just add content for specificblock.