Thoughts about Rails from a Django guy
lenni.info
lenni.info
I subscribe to the view that the template language is
for designers and should only allow safe constructs.
As someone who does both design and programming, I hate template systems that do this with a passion. I don't want to have to learn another language just so I can write templates. Especially when that language makes me jump through hoops to do various simple tasks the designers didn't anticipate--which is the case with every such language I've used.Erb is one of the nicer templating systems I've used. I've never had any trouble getting it to do exactly what I want it to do, and I've never had it behave in a way I didn't expect--which is another advantage to having it just use Ruby.
It doesn't have anything on Haml, though.
http://search.cpan.org/~sartak/Template-Declare-0.43/lib/Tem...
I certainly understand the use case of more-limited and embedded template languages, for working with designers or people less-comfortable with actual programming, or whatever, but when I'm just working on my own, or only with other programmers, Template::Declare works so much more nicely for me.
Why couldn't they just write it in python instead of inventing strange keywords like "ifequal"? Every time I use django templates, I have to look up documentation on how to do something as simple as string/number comparison. When I write Myghty templates, I'm writing python on top of html and it just feels so much nicer this way.
I felt confined and frustrated with Django until I realized that it was built with the intention that developers should just crank code on top of it. And not just page code - anything that you need. It's more than just an end product that is just configured, tweaked and templated. Of course ruby similar in the same way... But in comparison to java and .NET web frameworks this was a big change.
Why couldn't they just write it in python instead of inventing strange keywords like "ifequal"?
I think they were trying to get away from the horrid nastiness that had become php or before that cgi. Yet they still wanted to provide options for designers. It definitely has a learning curve and requires the programmer to just roll with it and adopt the "django way". At least until you figure out you can just move to another templating system.
http://docs.djangoproject.com/en/1.2/howto/custom-template-t...
http://docs.djangoproject.com/en/dev/ref/templates/api/#topi...
It's also interesting that in Django 1.2, they pulled away the nastiness of "ifequal" and "ifnotequal". Also, I don't buy their argument that the Django templating system is easier for "designers". Most designers whom I've worked with also do designs in other frameworks and Django's system is no more foreign than jsp, asp, or erb.
Agreed. I think they know that this is a weakness. I'd expect it to improve over time. In a similar fashion to them addressing ifequal and ifnotequal in 1.2.
Capitalization or minor string manipulation in erb doesn't require one to write a template filter function somewhere else.
Personally I'd prefer defining it once somewhere. Then use the function everywhere. Even with the initial code cost.
I think this is from past bad experiences. I once "repaired" a million plus line of code enterprise app that had misused some open source code that did date and number coercion (similar to django.contrib.humanize but for ASP). Needless to say the code was a mess and nearly the same code had been mixed in all over the place. The template code, the backend business logic and the asp C# files. After a few days of regexing then testing then fixing then searching then repeating. And then dealing with all the edge and special cases - I'll take a simple defined template function. All that being said - my example is a gnarly enterprise example involving bad code monkeys. So take it with a grain of salt - it's likely that the story isn't applicable to a lot of people here. But still - lesson learned. In the future designers can request template improvements and I'll deliver.
The unfortunate part is that many systems still use Django 0.9 and when one writes code there, you have to remember to use "ifnotequal" and "ifequal" as two different distinct keywords in their templating language. For example, if you were on google app engine and wanted to use their included django templating library, you'd be stuck with that horrible stuff.
On the other hand, when I'm building my own applications the Django template engine seems ridiculously bureaucratic, when I just want to drop in a Python function without reams of template tag boilerplate. That's one of the main reasons why I prefer Flask/Jinja2 for my side-projects (well that, and SQLAlchemy).
DataMapper took this approach initially, and was basically unusable in production because all it could do was create and drop tables. They've since added a handy upgrade method, and even ActiveRecord style migrations.
Personally, I like keeping the model and schema separate. I rarely need a giant list of columns at the top of my model. Luckily, Rails 3 will work great with DataMapper or Sequel (two fine alternatives to AR) if that's how you want to roll.
I haven't used DataMapper in a few years, but I remember their initial approach was to be smart and automagically add/remove any required columns/tables willy nilly.
Dependency management: pip requirements files. Migrations (which the author doesn't like, but are pretty useful on a non-trivial project): South Various other goodies: django-extensions
There are constant debates over whether XYZ should be in the core (see: South), or at least better discoverable, but if you're going to use a framework seriously it's worth it to evaluate as "framework + its stable and commonly accepted extras".
I like how django lacks all that directory structure and you can easily use a single file for your models.
Django templates are way more powerful and safer than erb / haml, etc.
I wish there was a port of django templates in ruby. If so my preferred environment would be sinatra + datamapper + django templates.
Ruty hasn't really been maintained, but it is pretty close to Django's templating system. Apparently, there's even been a little work to get it together with Sinatra (http://github.com/eladmeidar/sinatra-mvc/blob/master/app/hel...). I haven't used Sinatra, but I have played with Ruty and it's pretty nice and supports things like Django-style inheritance. It's a pity it never took off and this isn't a complete answer to your desire, but it might be an interesting place to look around.
"safer than erb / haml, etc"
Are you are referring to auto-escaping strings? If so, in Rails 3, this is on by default, and for Haml without Rails it's a simple setting.
I was referring to the auto-escaping as well as to the inability to run arbitrary code inside a template (for all I know this feature is added to the newest versions, though)
I have tried to get away from AR for a long time, with other ORMs such as DataMapper or Sequel, and more recently Mongoid. The AR in Rails 3 has adopted some of the good bits from both DM and Sequel, so that might make it more pleasant to work with.
In terms of templates, HAML/SASS are popular alternatives to ERB. For the admin backend; there are plugins for it.
Like the author, I too think Django's default directory structure doesn't work very well once you start doing bigger projects. But luckily, everything is so loosely coupled that you can change the directory structure entirely without too much work.
Some of it doesn't even work well for small projects--the first thing I do after ./manage.py startproject is change around settings.py so I can have local settings (namely db stuff) and not have to change things around when I push it to a server.
Pinax comes with quite a few example projects using a common directory structure, which is very well thought-out, separating media, reusable apps, common templates, etc.
This guy marks several advantages of Rails as weaknesses.
Migrations for example. In Django you have to migrate manually, or lose data. Lack of migrations is not an advantage.
Templates. The Django template language is one of the worst in existence. It is utterly inflexible and hard to use. It is slowly getting better: now you can use `if a == b` instead of `ifequal a b`. Why not go all the way to a usable and powerful template language?!
If you've been using Django "commercially", then how come you haven't heard of South? ... http://south.aeracode.org/
Everybody in the community that wants migrations is using it.
> Why not go all the way to a usable and powerful template language?!
In Django components are more decoupled than in Rails. You can replace that templating engine.
Also, Django's templating system is really not that inflexible or hard to use ... quite the contrary, it comes with many things out-of-the-box that aren't standardly provided by other web frameworks ... like the ability to cache page fragments.
Have you actually used Rails? Rails has fragment caching built in and you can use a different template engine very easily (just install the engine xyz you want to use and write templates with extension .xyz instead of .erb)
From my experience that's hardly the case ... ActiveRecord (pre 3.0 at least) sucks big monkey balls compared to Django's ORM. And Rails also doesn't have anything like the forms API in Django.
I only use django and have never-not used migrations for a project. Thanks god for South. http://south.aeracode.org/
"South brings migrations to Django applications." -http://south.aeracode.org/docs/about.html
as a general rule, i think you should probably do more than 1 simple project before evaluating any framework or language.
All I've seen are things like activescaffold, but that's not even anywhere near being comparable.