ERB templates

Contents
Render a string
Render a file
Trim mode
A layout sketch

Render a string

ERB mixes plain text with Ruby code inside special tags. Tags that start with an equals sign print a value into the output.

Call result with binding so the template sees local variables from the current scope.

require "erb" name = "Ada" puts ERB.new("Hello <%= name %>").result(binding)

Render a file

Real templates live in files, not in string literals. Read the file first and pass its content to ERB exactly as before.

Keep one template per file and give it an erb extension so editors highlight the mixed syntax.

require "erb" name = "Ada" template = File.read("hello.erb") puts ERB.new(template).result(binding)

Trim mode

Loops and conditions often leave stray blank lines in the output. Trim mode with a dash removes the newline after a tag.

The dash variant is the default choice for list templates where every extra empty line shows.

require "erb" items = [1, 2, 3] template = "<% items.each do |n| %>- <%= n %>\n<% end %>" puts ERB.new(template, trim_mode: "-").result(binding)

A layout sketch

Pages often share one frame with changing content in the middle. Render the inner part first, then print it inside a layout template.

The content variable carries the inner HTML while the layout adds the shared heading around it.

require "erb" content = "Hello" layout = "<h1><%= content %></h1>" puts ERB.new(layout).result(binding)

Next: WEBrick server

Article author: Arthur Isaev

Related articles
Ruby
Ruby course: zero to hero in 50 lessons
What is Ruby and where it runs
Install Ruby and check the version
irb: the interactive shell
Your first Ruby script
Variables
Numbers
Strings
Symbols
Truthiness: only false and nil
Conditions
Loops
Arrays
Hashes
Ranges
Methods
Blocks, procs, lambdas
String tools
Files and IO
Exceptions
Modules and mixins
Classes and objects
Inheritance
Enumerables
Duck typing and core idioms
Gems and Bundler
Testing with Minitest
Debugging
Encodings
Time and date
JSON and YAML
HTTP clients
Sockets
REST APIs
puts, print and p
Metaprogramming
Enumerators
Pattern matching
Arguments: splats and kwargs
Rake tasks
Environment and CLI
Logging
CSV
ERB templates
WEBrick server
Rails in one lesson
Capstone: todo CLI
Capstone: JSON API
Performance
Modern Ruby tour
Hero roadmap: the whole course on one page
Install Ruby on Windows 11
Install Ruby on Ubuntu
Install Ruby on Rocky Linux
Install Ruby on macOS
Install Ruby on FreeBSD
Ruby in Docker
Threads and fibers

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su, info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: