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.
