Community for developers to learn, share their programming knowledge. Register!
Views and Templating with ERB

Exploring the ERB Templating Language in Ruby on Rails


If you’re looking to expand your skills in Ruby on Rails, you can get training on this article, which delves into the intricacies of the ERB templating language. ERB, or Embedded Ruby, is a powerful tool that empowers developers to create dynamic views in Ruby on Rails applications. This article will explore the syntax of ERB, how to use it for dynamic content, the common tags you’ll encounter, and provide a comprehensive summary of its significance in Rails development.

Introduction to ERB Syntax

ERB is a templating system that allows you to embed Ruby code within a text document. It is particularly useful in Ruby on Rails, where it enables developers to write HTML files that can include dynamic content generated from Ruby code. The basic syntax of ERB is straightforward.

ERB files typically have a .html.erb extension and are processed by the Rails view renderer. The Ruby code is enclosed within <% %> tags, where:

  • <% %> is used for executing Ruby code without inserting it into the output.
  • <%= %> is used for executing Ruby code and inserting the result into the output.

Example of Basic Syntax

Here’s a simple example to illustrate the syntax:

<h1>Welcome to My Rails App</h1>
<p>The current time is: <%= Time.now %></p>

In this code snippet, the current time is dynamically displayed on the webpage. The <%= %> tag executes Time.now and outputs its result directly into the HTML.

Using Embedded Ruby for Dynamic Content

One of the main advantages of ERB is its ability to generate dynamic content based on application logic. This can range from simple variables to complex data structures like arrays and hashes.

Example: Looping Through an Array

Consider a scenario where you want to display a list of items. You might have an array of products in your Rails controller. Here’s how you can loop through the array and display each product name in a view:

# In your Rails controller
@products = ["Apple", "Banana", "Cherry"]
<!-- In your view (index.html.erb) -->
<h2>Product List</h2>
<ul>
  <% @products.each do |product| %>
    <li><%= product %></li>
  <% end %>
</ul>

In this example, the ERB syntax allows you to iterate over the @products array. Each product is output as a list item, showcasing how ERB can facilitate the rendering of dynamic content based on data passed from the controller.

Common ERB Tags and Their Uses

Understanding the various ERB tags and their applications is crucial for effective view rendering. Below are some common ERB tags and their typical uses.

1. Control Flow Tags

ERB supports control flow statements such as if, unless, case, and loops. This allows you to conditionally render content based on application logic.

Example: Conditional Rendering

<% if user_signed_in? %>
  <p>Welcome back, <%= current_user.name %>!</p>
<% else %>
  <p>Please sign in.</p>
<% end %>

In this example, the message displayed to the user changes based on whether they are signed in or not.

2. Comments

ERB allows you to add comments in your templates, which can be useful for documentation purposes. Comments are not rendered in the final output.

Example: Adding Comments

<%# This is a comment and will not appear in the output %>

3. Outputting HTML Safe Content

Sometimes, you may want to output HTML content without escaping it. You can use the html_safe method to achieve this.

Example: Outputting HTML Safe Content

<p><%= "<strong>Hello, World!</strong>".html_safe %></p>

This will render the string as HTML instead of escaping the <strong> tag.

4. Partials

ERB also supports the use of partials, which allows you to break up your views into smaller, reusable components. This promotes DRY (Don't Repeat Yourself) principles in your code.

Example: Using Partials

Create a partial file named _product.html.erb:

<li><%= product.name %></li>

Then render it in your main view:

<ul>
  <%= render @products %>
</ul>

This renders each product using the partial, making your views cleaner and more maintainable.

Summary

In summary, ERB is an essential component of Ruby on Rails that allows developers to create dynamic and interactive web applications. By mastering the syntax, dynamic content generation, and common ERB tags, you can significantly enhance your Rails views. The ability to embed Ruby code within HTML makes ERB a powerful tool for rendering content that is both flexible and responsive to user interactions.

By utilizing ERB effectively, you can improve your Rails application’s performance and maintainability. For further reading and detailed guidelines, you can refer to the official Ruby on Rails Guides. Through this exploration, we hope you feel more equipped to harness the power of ERB in your Ruby on Rails projects.

Last Update: 31 Dec, 2024

Topics:
Ruby on Rails