PHP vs Ruby, Why Rails just isn't the same in any other language
Posted by David Morton Sun, 22 Jan 2006 22:55:00 GMT
I just looked at another front runner to Rails, called CakePHP. In this example there is a fundamental concept that shows how beautiful Ruby is compared to PHP.
The view, as done by PHP:
<td><?php echo $post['Post']['id']; ?></td>
<td>
<?php echo $html->link( $post['Post']['title'],
"/posts/view/{$post['Post']['id']}" ); ?>
</td>
Now, that <?php echo stuff is rather verbose, and the method to access attributes is way too long. The equivalent section in Ruby:
<td><%= @post.id %></td> <td> <%= link_to @post.title, :action => 'view', :id => @post %> </td>
Now, tell me, which one is easier on the eye?

Hmm, both are kind of icky… I absolutely detest this jumping in and out of programming language. If you’re working with some right-brained designer type, they just aren’t going to get it. Here’s how I would rather see it…
<td>{[post_id]}</td> <td><a href=”/posts/view/{[post_id]}” rel=”nofollow”>{[post_title]}</a> </td>
Now for someone who only understands html, that’s pretty doggone understandable. They don’t even have to have a clue where the data comes from; all they need is a variable name.
Hello? what’s this “rel=’nofollow’”? I didn’t put that in there…
no, I disagree. You are still jumping in and out of a programming language, but now you are defining yet another language. So now, in addition to php/html/css/ whatever, now I have to learn a templating language? And it isn’t as powerful as one I already use in the project?
Also, ho do you set or name those variables? In smarty, I have dozens of lines setting them: $smarty->assign(“post_id”, 1);
Sometimes you can get sneaky by assigning an array, but still, that’s a lot of excess typing.
In Rails, I assign all those in one call:
<pre> @post=Post.find(:first) </pre>
or something like that. That’s all. Including another templating language just means more work and more typing. It violates the DRY principle. I hate repeating myself. :)
rel=’nofollow’ tells google not to follow the link, and thus renders the link useless for spambots to use to boost page rank.
For instance, how would you do this in a template language?
The loop, the helper functions… they all belong in the view, because the have to do with the way it is displayed. The controller or model do not need to be concerned with these details.
This way, we can have powerful helper functions to help: cycle through row colors to color the table, truncate a long entry to make it shorter, and format a date string to say “1 hour ago”. These functions are already built into rails. :)
gah. Something’s wrong with the comment system here, it isn’t supposed to mangle that so much.