Phone

+123-456-7890

Email

[email protected]

Opening Hours

Mon - Fri: 7AM - 7PM

In the vast landscape of web development, Embedded Ruby (ERB) emerges as a powerful templating engine, seamlessly blending HTML and Ruby to create dynamic web pages. 

While widely recognized as Rails’ default engine, ERB’s versatility extends beyond the framework, making it a valuable asset for various Ruby projects.

Mastering Templating for Dynamic Web Pages with Ruby ERB

ERB templates, identified by .html.erb or .erb extensions, encapsulate HTML with the potential for dynamic Ruby integration. The `<%= %>` tag plays a pivotal role, dynamically replacing content by evaluating enclosed Ruby code. Let’s delve into the syntax with a practical example:

```html

<!-- Example: Dynamic Greeting -->

<h1>

 Hello <%= name %>!

</h1>

```

In this example, the `<%= %>` tag dynamically inserts the value of the `name` variable into the HTML, showcasing the elegance of Ruby integration.

Exploring Embedded Ruby Templates

ERB’s strength lies in its ability to handle dynamic elements. Consider an ERB loop example:

```html

<!-- Example: Iterating Over Books -->

<% @books.each do |book| %>

 <div>

  <h2><%= book.title %></h2>

  <p><%= book.author %></p>

 </div>

<% end %>

```

This loop iterates over a collection of books, dynamically generating HTML for each, emphasizing ERB’s role in dynamic content creation.

Crafting Dynamic Web Pages with ERB

Beyond loops, ERB supports conditional statements. Here’s an example:

```html

<!-- Example: Conditional Statement -->

<% if @favorite_food == "chocolate" %>

 <p>Are you a chocolate lover? Indulge in our premium chocolate bars!</p>

<% else %>

 <p>Explore our top 10 snacks of the month.</p>

<% end %>

```

This illustrates ERB’s capacity to adapt content based on conditions, enhancing the dynamic nature of web pages.

Discover Ruby ERB tips here

Leveraging ERB Outside of Rails

While commonly associated with Rails, ERB’s utility extends to non-Rails projects. To use ERB independently, consider the following example:

```ruby

# Example: Using ERB Without Rails

require 'erb'

Book = Struct.new(:title, :author)

template = ERB.new(File.read('template.erb'))

result = template.result_with_hash(books: [Book.new("test"), Book.new("abc")])

puts result

```

This example demonstrates how to use ERB outside of Rails, rendering a template with data provided as a hash.

Comparing Ruby Templating Engines: ERB, HAML, and Slim

Performance and Syntax Benchmark

To assess performance, a benchmark using the Tilt gem compares ERB, HAML, and Slim. The results reveal ERB’s favorable execution speed:

```ruby

# Example: Benchmark

require 'tilt'

puts Tilt::ErubiTemplate.new("test.erb").render

puts Tilt::HamlTemplate.new("test.haml").render

puts Slim::Template.new("test.slim").render

```

The benchmark provides insights into the comparative performance of templating engines.

GitHub Repository Analysis: ERB, HAML, and Slim

Navigate the GitHub repositories of ERB, HAML, and Slim to gauge their popularity and maintenance levels. Key factors include stars, last commit, latest release, and creation date.

Choosing the Right Templating Engine

The decision to choose a templating engine involves personal preference and project requirements. Whether it’s ERB, HAML, or Slim, selecting the right engine is crucial for a seamless development experience. Consider factors like syntax, community support, and performance.

Conclusion

ERB stands as a dynamic force in templating, offering a blend of Ruby and HTML for crafting dynamic web pages. Armed with insights into syntax, examples, and a comparative analysis, developers can navigate the realm of templating engines with confidence. 

Recommended Articles

Leave A Comment