Rails new template file extensions

For some of you that don’t know this yet (don’t be ashamed of yourself, I only heard about it a a few months back), Rails 2 recommends a new file extension for your template file. At this point, everyone was using template extensions like:

  • .rhtml
  • .rxml
  • .rjs

To unify the template files, the Rails developers have come with a clever solution: changing the template extensions so that they math up and tell you in what format they are. By doing this, your new template extensions should look like this:

  • .html.erb
  • .xml.erb
  • .js.rjs

You are probably thinking: “everything looks alike now, except the rjs and haml templates. Why not using .js.erb? Well, the answer is quite simple.

  • .html.erb -> response format is in HTML,  parsed with ERB
  • .html.haml -> response format in HTML, parsed with HAML
  • .xml.erb -> response format in XML, parsed with ERB
  • .js.rjs -> response format in JavaScript, parsed with RJS

If someone ever came up with a plugin that does the same Javascript actions for some templates. Those templates could be called .js.dudesplugin . You see now? It all makes sense. Rails is all about convention, so it wouldn’t make sense to make an exception from the naming scheme for RJS templates.