Questions tagged [erb]

ERB is a simple templating system for Ruby, embedding code in any plain-text document. It is often used for HTML generation in web frameworks (such as Ruby on Rails).

ERB stands for Embedded Ruby and is a markup-based way of embedding Ruby code into text files.

The tag may be used for the standard (pure-ruby) ERB library, the C-based implementation eRuby or the even-faster Erubis library. ERB ships with Ruby installations as part of the Standard Library; the documentation for ERB is available on ruby-doc.org.

ERB is commonly used as templating system, for example in producing HTML pages with dynamic content (such as in the Ruby on Rails web framework) but is also useful in a variety of other contexts, such as code generation, mass emailing, or simple text reporting.

ERB markup is generally seen as <% ... %> and <%= ... %>. See the section "Recognized Tags" in the documentation for the ERB class for the full set of markup.

A simple example:

template.erb

Hello, <%=name%>. Are you:
<% moods.each_with_index do |mood,i|%>
  <%=i%>. <%=mood%>
<% end %>

code.rb

require 'erb'
template = ERB.new(IO.read('template.erb'))
name = "Phrogz"
moods = %w[ Happy Angry Sad ]

message = template.result(binding)
puts message

#=> Hello, Phrogz. Are you:
#=> 
#=>   0. Happy
#=>   1. Angry
#=>   2. Sad
2530 questions
385
votes
7 answers

What is the difference between <%, <%=, <%# and -%> in ERB in Rails?

Can some one please describe the usage of the following characters which is used in ERB file: <% %> <%= %> <% -%> <%# %> what's the usage of each one ?
simo
  • 20,548
  • 31
  • 101
  • 188
336
votes
6 answers

raw vs. html_safe vs. h to unescape html

Suppose I have the following string @x = "Turn me into a link" In my view, I want a link to be displayed. That is, I don't want everything in @x to be unescaped and displayed as a string. What's the difference between using <%= raw…
grautur
  • 27,957
  • 33
  • 90
  • 125
264
votes
7 answers

Best way to add comments in erb

How do we add comments in erb files, if we do not want them to be generated into the html content?
Kalyan Maddu
  • 3,957
  • 2
  • 19
  • 26
137
votes
6 answers

What is the meaning of erb?

Why is the view of Rails application in the format *.erb.html? What does "erb" mean?
khanh
  • 4,228
  • 10
  • 27
  • 47
121
votes
17 answers

Block comments in html.erb templates in rails

How do you comment out html mixed with ruby code? some text <% ... %> more text <%= ... %> something else <% ... %> In jsp it's real simple: <%-- ... --%>, but I'm unable to find any concise option in rails. Simple html comments do not…
Nikita Rybak
  • 64,889
  • 22
  • 150
  • 172
106
votes
5 answers

erb, haml or slim: which one do you suggest? And why?

I am learning Rails and I have seen these template engines. I have no experience with them (only erb). But as I am a beginner, I am really confused. Which one do you suggest and why? Erb, Haml or Slim? Please tell your reason for preferring one over…
Omid Kamangar
  • 5,580
  • 9
  • 35
  • 67
82
votes
1 answer

How do I escape the ERB tag in ERB

I have a simple fixture.yml file: label: body: "<%= variable %>" The issue is that the ERB code is parsed as part of loading the fixture, whereas I actually want the body to be literally "<%= variable %>" (un-interpolated). How do I escape the…
Daniel
  • 6,858
  • 5
  • 40
  • 46
63
votes
7 answers

Render an ERB template with values from a hash

I must be overlooking something very simple here but I can't seem to figure out how to render a simple ERB template with values from a hash-map. I am relatively new to ruby, coming from python. I have an ERB template (not HTML), which I need…
Shrikant Sharat
  • 6,838
  • 9
  • 49
  • 74
63
votes
6 answers

Include blank for first item in select list in options_for_select tag

I tried :include_blank => true, but it didn't work. If I need to add it to the collection, how would you do that?
B Seven
  • 39,557
  • 59
  • 208
  • 346
61
votes
6 answers

Default value for input with simple_form

im trying to do default value for input works ok: <%= f.input_field :quantity, default: '1' %> but i need f.input not f.input_field <%= f.input :quantity %> im trying it with standard html value - but after unsucessfull validation quantity is…
patie
  • 1,046
  • 1
  • 10
  • 18
57
votes
10 answers

Ruby templates: How to pass variables into inlined ERB?

I have an ERB template inlined into Ruby code: require 'erb' DATA = { :a => "HELLO", :b => "WORLD", } template = ERB.new <<-EOF current key is: <%= current %> current value is: <%= DATA[current] %> EOF DATA.keys.each do |current| …
ivan_ivanovich_ivanoff
  • 18,003
  • 24
  • 78
  • 100
55
votes
2 answers

What does <%== %> do in rails erb?

I saw this recently, thought it was interesting. But I don't really understand what it does? Ex. I have a rails app and I want to bootstrap some json, so that I don't have to make a second request. Normally I would write something like this. <%=…
mwoods79
  • 1,528
  • 12
  • 15
49
votes
7 answers

Notepad++ not syntax highlighting my files

Until a week ago I was happily coding html.erb files in Notepad++ with syntax highlighting. Then my hard drive crashed. I reinstalled Notepad++ on my new system but when I open my html.erbs, only a few of them are highlighting properly. I think the…
sscirrus
  • 50,379
  • 41
  • 125
  • 211
48
votes
1 answer

What is the difference between .erb , .rhtml and .html.erb?

What is the difference between .erb, .rhtml and .html.erb?
sameera207
  • 16,117
  • 18
  • 81
  • 143
47
votes
7 answers

Should I use haml or erb or erubis for potentially high traffic site?

I have been playing with Haml recently and really like the way the resulting code looks to me...the developer. I'm also not too worried about a designer being able to consume or change it...we're a small team. That said, beginning work on a project…
John Wells
1
2 3
99 100