4

When incorporating a third-party django app, I typically want it to be integrated aesthetically with the rest of my django project. While this is typically a matter of overriding the apps 'base.html' (if that), we all structure our templates a little differently, so incompatibilities often arise. For instance, suppose an app defines {% block footer %} and and makes use of it for a variety of things throughout its templates. If I am already using {% block footer %} for, say, a nav bar or copyright info, I don't want the app's templates to override my block.

A simpler, related case would be using different block names for the same thing. For instance, {% block extra-head %} versus {% block extrahead %}.

What's the best way of resolving these sorts of situations? Ideally, it would be nice to remap blocks, so you could do things like "put the child's {% block footer %} in the parent's {% block content-footer %}". Is there any way to get close to that? Or is the only solution to simply override every conflicting template?

Bryan Head
  • 11,575
  • 4
  • 28
  • 46

1 Answers1

2

First, the html inheritance should go:

my-sitebase.html
 |-- app-base.html
   |-- app-foo-template.html

I think this is what you meant, but the wording was a bit ambiguous. You might be able to just edit the app-base.html.

Second, a reusable app that overrides something like {% block footer %} is almost willfully causing trouble for anyone who uses it -- you should flag that in the provider's issue tracker.

If the app does do anything with {% block footer %} that should be done in the app-base.html area so you only have to change it once when integrating it with your site.

Finally, a recursive find-replace is actually very little effort. If you don't use an IDE that allows this, Text-Crawler is free, lightning fast, and a good non-IDE solution.

There have been a couple of attempts to create a standard inheritance pattern, I put together one that I like for the templates at djangoslingshot.com and I've seen one other -- but so far there hasn't been any coalescence around a standard. Likely because find-replace is actually a very low cost for the benefit of being able to do exactly what you want without someone else's rules getting in your way.

Ted
  • 11,304
  • 2
  • 28
  • 37