Ruby ERB Templating: Mixing Inputted Data and Ruby Together
Ruby is an extremely popular computer programming language first created in the mid-1990s that’s relatively simple to understand and easy to learn. Ruby’s companion web application development technology is Ruby on Rails, the platform from which popular web-based applications such as Twitter and Basecamp have been developed. You can learn more about these more advanced aspects of Ruby with an Udemy training course. For those who need a refresher on the basics, Udemy offers a thorough explanation of Ruby and Ruby on Rails, generally called just “Rails,” at its Ruby on Rails blog.
All Ruby programmers use Ruby ERB, or “Embedded Ruby,” at one time or another because the feature enables them to create any sort of text in any quantity from templates. Ruby ERB templates take the plain text that Ruby programmers enter, and then combine the text with Ruby code. When plain text is entered in the Ruby ERB feature, or template, it becomes easier to control the flow of data and character input as well as substitute variable characters and other items without worry.
The most common instance in which Ruby ERB is seen is when the programmer is using Ruby on Rails to create a Web page, because the ERB template can mix plain text, HTML and Ruby code together. A Ruby ERB template can take HTML, or “hyper-text markup language,” and meld it together with plain text and Ruby code to create any kind of text, including XML documents as well as RSS feeds. Ruby ERB can be extremely useful when you need to create a file containing many repetitions of a standard pattern of characters such as you might see in test answer sheets.
Ruby ERB Library
Ruby ERB is made up of several components, with its main component being its library. In Ruby you can “call up,” or retrieve or order up, Ruby ERB from a Ruby application or a Ruby Rake task. Ruby Rake is actually nothing more than a simple Ruby build program similar to Ruby Make. The library within Ruby ERB accepts any string or line of inputted characters as a template, and doesn’t impose any sort or rules or limitations on the source of that string for the template.
The Ruby ERB library is extremely flexible and when you’re programming using Ruby you can define a template built with ERB entirely within the parameters of your code. Plus, you can also store the ERB template in an external location such as a memory stick and then load it into your Ruby-written program as required. The beauty of Ruby ERB lies precisely in its flexibility and you’ll be able to store your templates in files or in SQL, or “structured query language,” databases on SQL servers or any kind of storage medium you’d like. Udemy offers several Ruby on Rails training courses that illustrate just how Ruby ERB, Ruby and Ruby on Rails work together to develop a quality web application, so take a moment to check one of those courses out.
ERB is Ruby’s Command-Line Utility
Ruby is open source, meaning its base code is free for the use of everybody, and many programmers take basic Ruby and customize it to a very high degree. But all Ruby distributions do come with what’s called a “command-line utility” that allows programmers to process templates in program files without having to write any additional code. It’s this command-line utility that Ruby calls “ERB.” You won’t need to install any software to make use of Ruby ERB, either, because it’s a part of the standard library found within all distributions of Ruby to date. Udemy’s advanced Ruby programming training course can put it all together for you once you’ve taken basic Ruby programming, and show just how Ruby’s command-line utility can greatly shorten your programming learning curve.
Writing Templates and Ruby ERB
Ruby ERB basically copies the text portion of a template directly to a generated document and only processes the code that’s identified by “markers” or “tag markers.” Markers in Ruby ERB cause the code entered to be handled in a specific way. Here’s an example of what Ruby ERB would look like when a template is being created:
1 Hello, <%= @name %>.
2 Today is <%= Time.now.strftime(‘%A’) %>.
In the Ruby ERB activity example above, a tag with an “equals” sign indicates that the enclosed code is what’s called an “expression.” The equals sign tells the rendering program to substitute the code element with the code result as a string or line of inputted characters when it actually renders or creates the template. In Ruby ERB you use an expression to place, or “embed,” a line of code into the actual template or to display the contents of a variable, as shown in the example above.
Tags that don’t contain an “equals” sign denote or show that the enclosed code is what’s called a “scriptlet.” Every scriptlet in a Ruby program is caught by the program and then executed or utilized, with the final result of the code then being inserted or “injected” into the outpoint at the point where the scriplet appears, as illustrated in this example:
2 <% for @item in @shopping_list %>
3 <li><%= @item %></li>
4 <% end %>
All of the scriplets in the above example enclose an expression. The scriptlets themselves don’t produce text, but do cause the expressions to run multiple times. The result of each expression is then written to the output each and every time. Because Ruby is uses objects to perform tasks, rather than data and a logically made decision as would be the case in a more traditional computer program, it’s called on OOP or “object-oriented program.” Ruby ERB, like all such functions within Ruby, will create objects to perform tasks. In this case, the tasks to be performed are creation of templates.
So, upon what’s called “object instantiation” — or the effort to create a template — ERB will look through each line of the templating string or line of inputted characters for specific patterns. Here are the two most important patterns to Ruby ERB:
1 <% “ERB will evaluate this!” %>
2 <%= “ERB will evaluate and output this!” %>
The syntax in the above example is actually very easy to read and then understand. And Ruby ERB creates the syntax and makes all of this activity happen seamlessly and without any fuss, which is yet another reason why Ruby and Ruby on Rails are so popular among programmers, Web developers and coders. Ruby ERB is also especially valuable when developing a web-based application in Ruby on Rails.
Many online games or instant messaging and social media applications such as Twitter use Ruby on Rails, for example, because it’s so easy to develop an application using the development technology that’s based on Ruby. You can also learn Ruby on Rails from scratch, with no programming experience required, through Udemy.
In fact, Udemy offers several basic Ruby and Ruby on Rails learning courses that teach ERB and thoroughly review all of its uses and how simple Ruby and Rails are to actually utilize when writing computer programs and web applications such as Twitter. If you’re really interested in high-quality computer programming that’s both easy to learn, to write in and then maintain you should check out a basic Ruby programming course from Udemy right now.
Ruby students also learn
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy for Business.