views:

227

answers:

2

I've been developing in the Symfony framework for quite a time, but now I have to work with Django and I'm having problems with doing something like a "component" or "partial" in Symfony.

That said, here is my goal:

I have a webpage with lots of small widgets, all these need their logic - located in a "views.py" I guess. But, how do I tell Django to call all this logic and render it all as one webpage?

+3  A: 

It sounds like what you're looking for is something like custom template tags...

You can write your own set of tags that process custom logic and return template chunks that are reusable in a very widget-like way.

Gabriel Hurley
Yes, seems like this is the exact way to do this in Django. Thanks a lot!Seems weird, but it's an effect of this whole "templates for non-devs" I guess...
ktoso
A: 

Assuming you are going to be using the components in different places on different pages I would suggest trying {% include "foo.html" %}. One of the (several) downsides of the Django templating language is that there is no concept of macros, so you need to be very consistent in the names of values in the context you pass to your main template so that the included template finds things it's looking for.

Alternatively, in the view you can invoke the template engine for each component and save the result in a value passed in the context. Then in the main template simply use the value in the context.

I'm not fond of either of these approaches. The more complex your template needs become the more you may want to look at Jinja2. (And, no, I don't buy the Django Party Line about 'template designers' -- never saw one in my life.)

Peter Rowell