What is the difference between <%, <%=, <%# and -%> in ERB in Rails?

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for What is the difference between <%, <%=, <%# and -%> in ERB in Rails?

๐Ÿ“ข Calling all Ruby on Rails developers! ๐Ÿš€ If you've ever found yourself scratching your head trying to understand the mysterious symbols in ERB files, fret not! ๐Ÿ™Œ In this blog post, we'll dive into the differences between <%, <%=, <%#, and -%> in ERB in Rails. We'll break it down, provide practical examples, and help you solve any issues you may encounter. Let's get started! ๐Ÿ’ช

<%

Let's kick things off with <% - the humble ninja of ERB tags. When you want to include Ruby code in your ERB template without rendering any output, this is the go-to tag. Use it to perform calculations, conditions, or loops. Think of it as your trusty backstage technician, pulling the strings behind the curtain. ๐ŸŽญ

Here's an example:

<% name = "John" %>
<% if name == "John" %>
  <h1>Welcome, <%= name %>!</h1>
<% else %>
  <h1>Hello, anonymous friend!</h1>
<% end %>

In this snippet, we assign the value "John" to the variable name. The <% if name == "John" %> block ensures that the personalized welcome message is only displayed when name matches "John". Goodbye, anonymous friend! ๐Ÿ‘‹

<%=

Say hello to <%= - the ERB tag with a touch of showmanship! Use it when you want to display the output of a Ruby expression in your ERB template. It's perfect for injecting dynamic content like variables, method calls, or even calculations directly into your HTML. ๐ŸŽฉโœจ

Here's an example:

<% name = "John" %>
<h1>Welcome, <%= name %>!</h1>

In this case, the <%= name %> expression dynamically inserts the value of the name variable into the HTML output, resulting in a personalized greeting. Magic! ๐ŸŽฉ๐Ÿ’ซ

<%#

Let's have a look at <%# - the ERB tag that's as silent as the night. It's used for comments, helping you document your code and provide context to other developers (or future-you). These comments are not displayed in the final rendered view, making it a perfect tool to explain and organize your code. ๐Ÿ“

Here's an example:

<%# This block calculates the total price %>
<% price = 19.99 %>
<p>The total price is <%= price %> dollars.</p>

In this snippet, the <%# This block calculates the total price %> comment clarifies the purpose of the following code. It's like leaving little breadcrumbs to guide others through your codebase. Helpful! ๐Ÿž

-%>

Last but not least, let's talk about -%> - the ERB tag that adds a touch of elegance by stripping trailing whitespaces. When you want to remove any unwanted spaces or blank lines right after your ERB tags, this is your secret weapon. It's especially handy when working with HTML or generating structured documents. โœ‚๏ธ๐Ÿ•ถ๏ธ

Here's an example:

<% name = "John" -%>
<h1>Welcome, <%= name -%>!</h1>

By adding - immediately after the opening <% tag, the trailing whitespace is gracefully trimmed. This ensures that no unwanted spaces or newline characters make it into your rendered output. Squeaky clean! ๐Ÿงผโœจ

Wrapping Up

And there you have it! You're now equipped with the knowledge of the differences between <%, <%=, <%#, and -%> in ERB in Rails. ๐Ÿ’ก Remember, <% is for Ruby code without rendering output, <%= is for displaying Ruby expressions, <%# is for comments, and -%> removes trailing whitespace. ๐ŸŽ‰

So go forth, conquer those ERB templates, and make your views shine like never before! If you have any questions or want to share your newfound ERB expertise, drop a comment below. ๐Ÿ‘‡ We'd love to hear from you! Happy coding! ๐Ÿ’ป๐Ÿ’–

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

๐Ÿ”ฅ ๐Ÿ’ป ๐Ÿ†’ Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! ๐Ÿš€ Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings ๐Ÿ’ฅโœ‚๏ธ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide ๐Ÿš€ So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? ๐Ÿค” Well, my