Skip to Content
Ruby on Rails® for Microsoft Developers
book

Ruby on Rails® for Microsoft Developers

by Antonio Cangiano
April 2009
Intermediate to advanced content levelIntermediate to advanced
479 pages
12h 55m
English
Wrox
Content preview from Ruby on Rails® for Microsoft Developers

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

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Ruby on Rails™ 3 Tutorial: Learn Rails™ by Example

Ruby on Rails™ 3 Tutorial: Learn Rails™ by Example

Michael Hartl

Publisher Resources

ISBN: 9780470374955Purchase book