Powered

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?

Posted in  | 6 comments

Comments

  1. Tim Morton said 1 day later:

    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.

  2. Tim Morton said 1 day later:

    Hello? what’s this “rel=’nofollow’”? I didn’t put that in there…

  3. dgm said 1 day later:

    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. :)

  4. dgm said 1 day later:

    rel=’nofollow’ tells google not to follow the link, and thus renders the link useless for spambots to use to boost page rank.

  5. dgm said 1 day later:

    For instance, how would you do this in a template language?

    
    &lt;% for post in @posts %&gt;
    &lt;tr class="tablerow&lt;%= cycle ["1","2","3"] %&gt;"&gt;
      &lt;td&gt;&lt;%= post.title %&gt;&lt;/td&gt;
      &lt;td&gt;&lt;%= distance_of_time_in_words(post.posted_on %&gt;&lt;/td&gt;
      &lt;td&gt;&lt;%= truncate post.body %&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;% end %&gt;
    

    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. :)

  6. dgm said 1 day later:

    gah. Something’s wrong with the comment system here, it isn’t supposed to mangle that so much.

Comments are disabled