Druby on Rails part 2

And now something positive.

Well, not that the past two articles were nagative. But they focussed on what RoR does and Drupal does not. They focused on parts in Drupal that could use improvement.

Drupal is not all that bad. Not at all. Drupal is fantastic. So I would like to highlight one of Drupals jewels, and compare it to Ruby on Rails. Just to show you that for certain tasks Drupal development hardly differs from RoR development.

We all know PHPtemplate, and if not: its a templating engine for Drupal that takes all the code and logic out of the view layer. It allows for very quick and easy templating, using native PHP. That, in itself is not too special. The fact that this PHPtemplate is highly optimised for templating is.

Just compare the two:

<table>
<% for product in @products %>
  <tbody>
    <tr valign="top">
      <td>
        <img src="<%= product.image_url %>" border="0" />
      </td>
      <td width="450">
        <h3><%=h product.title %></h3>
        <small>
          <%= product.description %>
        </small>
        <br/>
        <strong>€<%= sprintf("%0.2f", product.price) %></strong>
        <% link_to  'Add to Cart',
                    :action => 'add_to_cart',
                    :id   => product %>
        <br/>
      </td>
    </tr>
    <tr>
      <td colspan="2"><hr /></td>
    </tr>
  </tbody>
<% end %>
</table>

And here is how Drupal does it in a theme function

<table>
<?php  foreach ($products as $product): ?>
  <tbody>
    <tr valign="top">
      <td>
        <img src="<?php $product->image_url ?>" border="0" />
      </td>
      <td width="450">
        <h3><?php print check_plain(product->title) ?></h3>
        <small>
          <?php print product->description ?>
        </small>
        <br/>
        <strong>€<?php print theme('store_price', produc->price) ?></strong>
        <?php print l('Add to Cart',
                'add_to_cart/'. $product->id
                );
        ?>
        <br/>
      </td>
    </tr>
    <tr>
      <td colspan="2"><hr /></td>
    </tr>
  </tbody>
<?php endif; ?>
</table>

What we see, is a non existing example. But it could easily exist like this. You see the similarities? The simplicity of both examples is stunning. The main difference lies in the fact that Drupal is not as file-centered. So you will have to addsome code to a template.php file before you can use a template file. Here lies a task, since Drupa is able to pull template functions from files. It is just not yet as obvious. We should ease this process.

Conclusion, however, is that Drupal has a lot of similarities with RoR.

This article was published on webschuur.com. And migrated to this blog.

in theming25 drupal214 ruby25

About the author: Bèr Kessels is an experienced webdeveloper with a great passion for technology and Open Source. A golden combination to implement that technology in a good and efficient way. Follow @berkes on Twitter. Or read more about Bèr.

blog comments powered by Disqus