9.2. Built-in Template Engines

A template enables the view to dynamically generate the response's body by combining dynamic values, like instance variables and the flash object, and static content like HTML tags and text. Throughout this book you've been exposed to ERb templates as a means of generating XHTML documents. Yet ERb is not limited to this task and can be used to generate any other type of document that requires a mix of static and dynamic content (that is, Ruby code).

For the sake of convenience, Rails ships with two other built-in template engines: Builder and RJS. And whereas ERb is normally used for XHTML pages, Builder is particularly handy for XML content and RJS for writing JavaScript responses in Ruby code.

To tell Rails what document type is going to be rendered, and what template engine is going to be used, you add two extensions to each template: myfile.html.erb for XHTML produced through ERb, myfile.xml.builder for XML produced through Builder, and myfile.js.rjs for JavaScript produced through RJS.

Note that for historical reasons, the old extension format is still accepted. If you work with legacy code, you may find the equivalent myfile.rhtml, myfile.rxml, and myfile.rjs. As you upgrade an existing project to Rails 2.2 or a newer version, you can safely rename them.

9.2.1. ERb

ASP.NET developers are used to thinking in terms of controls that are positioned within a page. In ERb templates, the approach is entirely different. In fact, most templates contain ...

Get Ruby on Rails® for Microsoft Developers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.