SHARE:
Uncategorized

What is ERB?

Flatiron School / 25 October 2012

The following is a guest post by Ericka Ward and originally appeared on her blog. Ericka is currently a student a The Flatiron School. You can learn more about her here, or follow her on twitter here.

ERB stands for Embedded Ruby. ERB is a templating system for Ruby. It allows you to embed Ruby into a text document.

I recently had my first encounter with ERB while working with some classmates on a project. The goal was to make the student profile pages on our school website dynamic. In our first attempt to make the profile pages, we hard coded the HTML for each student. We have 20 students in the class so that was pretty annoying and repetitive. In our last attempt, we used an ERB file in order to make our HTML pages dynamic.

Below is an example of our hard coded HTML:

The code below is an example of erb. It will render the same HTML page:

In the example above, @student_info is an instance of the Student class and we are calling methods on that instance in order to extract the attributes and input them into the HTML. In the code snippet above, <%= %> is the ERB tag. This specific ERB tag is used when the embedded code is an expression and you want the result of the expression to render in the view. There are other ERB tags. For example, if you do <% %> (leaving out the equal sign) the code within the ERB tag will execute, but the evaluation will not render in the view.

As I learn more about erb, I will add more posts about the details of ERB, but this is a really good start for my fellow beginners. You should now understand the core of what ERB does.

Here are some more great resources on ERB:
http://cheat.errtheblog.com/s/erb/
http://www.stuartellis.eu/articles/erb/
http://ruby-doc.org/stdlib-1.9.3/libdoc/erb/rdoc/ERB.html

3 Reasons to Quit Your Job and Learn to Code Previous Post 7 Easy Steps to Start With TDD Next Post