views:

143

answers:

3

Hi all,

I know i can have common stuff like this in django

<html>

{% include "header.html" %}

<div id = 'content'>
blah
</div>

{% include "footer.html" %}
</html>

but it seems i need to pass data footer.html and header.html every time i include these pages?

Thanks

+3  A: 

Django's template inheritance may be just what you want in terms of not repeating the includes and other boilerplate. If by "pass data" you mean in the rendering context, e.g. there is some data you always want to put there, simplest is to make all your rendering contexts with a factory function that fills in the common parts of the data.

Edit: for the factory function I have in mind nothing especially fancy, just e.g. a simple class:

class ContextFactory(object):
  def __init__(self, **pervasive):
    self.pervasive = pervasive
  def makeContext(self, current):
    return dict(self.pervasive, **current)
  def registerPervasive(self, name, value):
    self.pervasive[name] = value

(etc, if you need more functionality there) then instantiate a single instance contextFactory in some appropriate module of yours. Where your views might currently be rendering with context dicts such as {'foo': bar}, you will instead use contextFactory.makeContext({'foo': bar}) to give the context factory a chance to inject whatever name/value pairs are currently registered with it -- that's all, really.

Alex Martelli
thanks a lot, is there detailed documentation about "make all your rendering contexts with a factory function"?
no, I haven't seen documentation (detailed or otherwise) about this specific application of them, but "factory" design patterns are well known since the Gang Of Four, and this is quite a simple case: instead of just passing a literal dict as the context, call your own makeContext function (with the dict as the argument) and it will enrich the dict, if and as needed, with whatever "pervasive data objects" you have registered with it (which it keeps as another dict) -- it basically just merges the two dicts by `return dict(self.pervasive,**current)`!
Alex Martelli
A: 

This also interests me as a newcomer to Django with ASP.NET background. I'm interested in making 'standalone' components that can be included in some views, but I don't want to add data for them in every view method if they're not going to be needed in the view. If I need to construct a context object that knows about data needed by all possible includes then things get more coupled.

I noticed somewhere else that this might be possible to do with a custom tag that sets data for the included template. It seems a cleaner solution to me even if it needs some more programming. An example of usage can be found at http://docs.djangoproject.com/en/dev/ref/contrib/comments/#ref-contrib-comments-index

rslite
A: 

See http://stackoverflow.com/questions/1052317/why-on-earth-do-i-have-to-pass-requestcontext-in-all-of-my-responses

Apparently this is such a common pain that there's a package called django-annoying to fix that (among other things).

tpk