Home

erb

ERB, short for Embedded Ruby, is a templating system that is part of the Ruby programming language. It allows Ruby code to be embedded directly into text documents, most commonly HTML, to generate dynamic content. When rendered, the template is converted into Ruby code and evaluated within a given binding, producing a final string.

Syntactically, ERB uses tag delimiters to distinguish code from text. The most common are <% ... %> for Ruby

ERB files typically have the .erb extension and are used in Ruby applications and frameworks such as

Security and escaping considerations apply when rendering HTML. ERB itself does not automatically escape HTML special

code
that
is
executed
without
producing
direct
output,
and
<%=
...
%>
for
expressions
whose
results
are
inserted
into
the
template.
Whitespace
and
trimming
can
be
controlled
with
variants
like
<%-,
-%>,
which
remove
surrounding
whitespace.
Ruby
on
Rails.
To
render
a
template
in
Ruby,
you
require
'erb',
read
the
template
text,
create
an
ERB
object
with
ERB.new(template),
and
call
result(binding)
to
produce
the
output.
The
template
can
access
variables
and
methods
available
in
the
binding
at
render
time.
characters;
frameworks
often
provide
escaping
helpers
or
use
additional
layers
to
mitigate
cross-site
scripting.
ERB
remains
a
foundational
tool
for
Ruby-based
templates,
with
alternative
templating
engines
like
Haml
and
Slim
available
if
different
syntax
or
features
are
desired.