Thursday, February 09, 2006

Python: Django Templates

"As simple as possible, but no simpler."

I have a lot of respect for what the Django guys have done. I actually really like a lot of Django. However, having tried it, I find the templating engine to be elegant, but too simple. What frustrates me most is the lack of functions. In my first programming class, I learned how to use functions for code reuse. DRY applies even to HTML. At my company, even the "template authors" know how to create a function to avoid code duplication. Nor is the "include" or "block" statement a suitable replacement--functions have parameters. Different arguments lead to different results. This is necessary functionality. Also, functions should support recursion. I learned in my second programming class how to think recursively, and every once in a while it really comes in handy (for instance, when you're writing a bulletin board application with a hierarchy of messages).

When I was using Django templates, I really felt like my hands were tied. I don't think lack of functions improves security. I don't think custom tags are always a suitable replacement--I'm talking about simple HTML reuse. Please forgive my boldness, but I don't think I should have to live without functions just because a template writer working at some other company doesn't understand them.

Next, I think I'll try out using Django with Cheetah. So far, Cheetah has always met my needs exceedingly well. Hopefully, I can get it to fit in smoothly, maintaining Django's overall elegant feel.

7 comments:

Jason Huggins said...

There is no requirement that you use the Django templates, though they are the "official" way that show up in all the docs (which is a good thing).

You might want to try using Myghty with Django... It looks like Myghty gives you the most freedom to define those functions in those templates like you want to. I say "looks like", though, because I haven't used, just skimmed the docs... If Myghty is "just a python library" like Cheetah is, then the integration of Django and Myghty might be pretty easy. So check out Myghty and let me know if I'm way wrong on this.

Shannon -jj Behrens said...

Jason. I agree with you, although I'll probably use Cheetah instead. I *like* Myghty, but I've been using Cheetah for two years now. We have well over 100,000 lines of Cheetah company-wide, and I really like it.

Anonymous said...

Actually you can do that stuff quite easily with Django - just produce some custom tags based on the inclusion tags stuff (like it is used in the admin). What this gives you essentially are template-based functions - only that they are written as tags.

Django does have a quite flexible template language - you only need to accept that to do more complex work, you either precompute stuff in the view function or build custom template tags.

Jay Graves said...

I agree with you Jason. I understand that the Django templates are meant to be simple so designers can understand them. But just the other day the designer I work with came to me because he wanted to use and OR with the ifequal tag. Django doesn't support this, so I submitted a patch. The resulting feedback let me know that this was not wanted.

I thought about switching to Cheetah for a while, but I enjoy Django and I am still doing things the Django way.

However I have started to create my own library of custom tags that are more robust than the one Django offers.

lalo said...

I felt the same on my first contacts with those templates. But then I had an epiphany. Actually trying to develop with Django made me move all my logic out of the templates and into python (into the view), which is something I have been wanting to do for years.

So for your board problem, I recommend you try that approach rather than changing the template engine; rather than using render_to_response, do the calling and recursing in python, and make calls into the template engine to turn your data into html snippets.

Shannon -jj Behrens said...

Quoted:
I felt the same on my first contacts with those templates. But then I had an epiphany. Actually trying to develop with Django made me move all my logic out of the templates and into python (into the view), which is something I have been wanting to do for years.

I hear what you're saying, but I truly believe that even template authors should not be exempt from DRY.

Anonymous said...

I don't think that's what he's saying... using a loop inside the view isn't repeating anything. You're just using the templating engine in a slightly different way. Instead of preparing all of your data, then making a single template call, make smaller template calls as you need them and string the whole thing together with a final render.