Worse is Better: Python templating systems vs. Rails ERb

December 3, 2006 at 6:42 pm 1 comment

(Disclaimer: Python is my favorite programming language; however, I do criticize it in this article. Please redirect flames to /dev/null.)

I’ve been comparing web frameworks lately, developing a relatively simple CRUD database-backed application that can support a few bells and whistles. I’ve checked out Ruby on Rails (who hasn’t?) Django, and Turbogears in my search, and realized something as I was hacking together some templates in Kid:

Python, a far more restrictive and inflexible language – one that would seem, on the surface, to be worse for web application development than Ruby – is better for programmers, designers and security by nature of its inflexibility.

Let me explain what I mean. When designing ‘views’ – the web pages that users will see – that need to be updated with data from a given database, it is natural to break the Model-View-Controller paradigm and embed some controller logic into the application. After all, it’s so much less effort to embed

<% found_books = Book.find_all(“title = ?”, given_title) %>

into your .rhtml files then to go back into the controller file, change the return types, and make sure everything works correctly. However, Python is whitespace-sensitive, and therefore is much more difficult to embed into HTML. As such, you can’t write the mixture of Ruby and HTML that makes Rails so easy to use.

This is a good thing. I am a strong advocate of separating the model, view, and controller; by the very fact that I can’t write a combination of Python and HTML, I am forced to go back into the CherryPy/Django controller codebase and write a properly formed, secure, and elegant SQL query. Normally laxity wouldn’t bother me, but I believe that any large web application will grow into a hideous, Nylartothepic tangle if the MVC rules are not followed to the letter.

I still love Rails, but I love the templating systems in Python even more. Because Python is so restrictive, people had to write templating systems the Right Way – and that’s a good thing for everybody.

Entry filed under: django, mvc, python, rails, ruby, templating, turbogears, webapps. Tags: .

Fancy Windows, &c. It’s Hard Out Here for an MVC Advocate

1 Comment

  • 1. Sam  |  July 7, 2008 at 11:48 pm

    I have always found the line between “business logic” and “template logic” hard to define but I am quite sure the way to separate these concerns has nothing to do with the power of the template language. Instead, I believe in the following rule: templates are pure functions. A template is not allowed to modify the callers environment, nor is its output dependant on any state not explicitly passed to it. Its sole purpose is to transform input to output and it can use any algorithm or language to do so as long as the template remains referentially transparent. This gets a bit fuzzy with ‘magic’ since @post.author.name may or may not need a database query depending on whether the :include directive was used in the controller e.g. @post = Post.find … :include => :author. Whether or not you consider a second database read referentially transparent depends on the application but since it is not atomic with the first it is always possible that the author’s name was changed between controller and template execution. In summary, db reads in the template are bad but Rails encourages them during development. When the app settles into a more final form it is best to add the :include directive in the controller for atomic reads and performance. Templates as pure functions is a pretty easy rule to enforce. Easy enough that there is no reason to limit the template language, and as a bonus pure functions have a good definition unlike the silly idea of “business logic”. What’s that anyway?

    Blogged about it.
    http://samdanielson.com/2007/9/11/templates-are-pure-functions


About Me



I'm Patrick Thomson. This was a blog about computer programming and computer science that I wrote in high school and college. I have since disavowed many of the views expressed on this site, but I'm keeping it around out of fondness.

If you like this, you might want to check out my Twitter or Tumblr, both of which are occasionally about code.

Blog Stats

  • 650,375 hits

%d bloggers like this: