Entries published on June 7, 2006
Django tips: Write better template tags
Django‘s template tags are a great way to handle things that don’t always make sense being in a view. If you want to have, say, a list of recently-added content which appears in a sidebar or footer on every page of a site, it’d be crazy to manually change every view to fetch that content and add it to the template context; a template tag is definitely the way to go.
For example, in the footer of every page on this site, I pull out the five most recent entries, five most recent links and five most recent comments, and it’s all done with template tags. Now, at first it might look like there are only two ways to do this:
- Write three different template tags: one for entries, one for links and one for comments. This is annoying because it’s basically repeating the same logic three times and only changing …