DEV Community

loading...
Cover image for Embedded RuBy (ERB)

Embedded RuBy (ERB)

spenser6131 profile image Spenser Brinkman ・2 min read
closing_statement = "Thanks for reading!"

An effective web developer's toolkit should incorporate a diverse and practical range of technologies that can be coordinated to achieve a robust final product. One such example of a technology at the disposal of a programmer is Embedded Ruby. There are a few popular implementations of Embedded Ruby. One such implementation is included in the Ruby standard library and is called "ERB".

Through ERB, one is able to write dynamic text files (such as HTML documents to be viewed as web pages) using integrated Ruby code and logic.

Exempli Gratia

Let's say I wanted a web page to display something like this:

"Hello, World!" displayed ten times

I could write an HTML document as such:

HTML code to display "Hello, World!" ten times

But with the power of ERB, I can achieve the same results by having my application require the 'erb' gem and writing the following code in a .erb file*:

ERB code to display "Hello, World!" ten times

In the example above, you might notice two unfamiliar tags:

  • Execution tags, represented with <% some ruby code %> (also known as ice cream cones) to create what are called "scriptlets"

and

  • Expression tags, represented with <%= some ruby code %> (otherwise aptly called squid tags) to create "expressions"

Scriptlets are the purest form of embedded Ruby, in that they covertly execute the enclosed Ruby code upon any HTML elements indicated. A user won't ever actually see the results from a scriptlet, but they will see the effects they bring upon the HTML elements being executed upon. Expressions, on the other hand, will run Ruby code and display, as a string, the returned value of said code.

In other words, going off of the above example, line 1's scriptlet initiates a loop to be run 10 times. The only code within our loop's block is an expression on line 2 which returns a literal string of "Hello, World!" followed by an HTML <br> tag to create a line-break. Line 3 wraps up the code with another scriptlet, this time indicating the end of our loop's block.

In Summary

This elementary example is just a taste of the power of ERB. Any code that can be written in a plain .rb file can also be interpreted in scriptlets and expressions. A programmer could, theoretically, write the entire codebase for their ruby-based web application all within a single .erb file.**

Documentation for ERB is as extensive as the technology itself is powerful, so if one means to take advantage of it's utility, there are many avenues to research, such as the rubyonrails.org RailsGuide

closing_statement = "Thanks for reading!"

<%= closing_statement %>

*Convention often dictates that the file should be ended with the document type followed by .erb, as in "example_file.html.erb". This helps to avoid confusion about exactly what type of document is being acted on by Ruby code without having to open the file to investigate.
**Writing an entire application in a single .erb file is not recommended.

Discussion (0)

pic
Editor guide