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
2617 questions
21
votes
1 answer

YAML with erb is not parsing

Why this yaml file won't parse? --- <% sensor_types = YAML.load_file('db/seed-fixtures/sensor_type.yml') %> <% sensor_types.each do |sensor_type| %> sensor<%= sensor_type['id'] %>: id: <%= sensor_type['id'] %> title: <%= sensor_type['title'] %> …
kirlev
  • 680
  • 1
  • 7
  • 17
20
votes
3 answers

Rails js.erb file cannot find method "render"

I've got the following code in a Javascript ERB file: $(document).ready(function() { $("#workout-week").append( <%= escape_javascript(render :partial => "show_training_period", :locals => { :period => @period }) %> ); }); When I got to the…
Mike
  • 19,267
  • 11
  • 56
  • 72
20
votes
3 answers

Rails ERb best practices (<% %> vs <% -%> vs <%- -%>)

What is the recommended use of ERb in Rails when it comes to <% %> (evaluate Ruby code), <% -%> (evaluate Ruby code, suppress the trailing newline) and <%- -%> (evaluate Ruby code, suppress the trailing newline and leading space)? It seems like <%-…
jrdioko
  • 32,230
  • 28
  • 81
  • 120
20
votes
1 answer

Rails ERB <%- ... -%> vs. <% ... %>

Possible Duplicate: Difference between -%> and %> in rails So for an ERB file, what's the difference between <%- ... -%> vs. <% ... %>? As far as I can tell, they serve the same purpose but one requires more typing than the other.
Bob
  • 8,424
  • 17
  • 72
  • 110
19
votes
1 answer

Specifying format: "xml" ignored with render_to_string

I have an action that needs to render a view to string. The view is called index.xml.erb. I am trying to achieve this with render_to_string: my_string = render_to_string(layout: false, format: "xml") render_to_string is instead rendering the…
YWCA Hello
  • 2,997
  • 4
  • 29
  • 40
19
votes
1 answer

Ruby ternary operator in erb?

How can I make this code look better: <%=raw manuscript.uploaded_to_s3? ? "" : "" %> That is, can the HTML go outside of the ERB block making this easier to read?
Reed G. Law
  • 3,897
  • 1
  • 41
  • 78
19
votes
4 answers

Passing binding or arguments to ERB from the command line

I have been playing around with erb from the command line recently. I wanted to make a dirt simple erb template, for example the following: <%- name = "Joe"; quality = "fantastic" -%> Hello. My name is <%= name %>. I hope your day is <%= quality…
scott_fakename
  • 10,219
  • 2
  • 15
  • 20
18
votes
2 answers

Remove HTML tag attribute in slim when attribute should not be displayed

I would like to remove class attribute when class should not be displayed in Slim. In ERB, I could use: /> How do I do this in Slim? I found this, but I feel there must be a more idiomatic solution: |…
Piioo
  • 759
  • 10
  • 24
17
votes
3 answers

Avoid *.js.erb files by building all the asset_path values

So I want to avoid processing JavaScript files with ERB just so I can get a proper asset path to, say, an image. Currently, this seems like the popular approach: var myImage = "<%= asset_path('my_image') %>"; Which, of course, requires the…
Justin Searls
  • 4,789
  • 4
  • 45
  • 56
17
votes
3 answers

Rails - default value in text_field but only for new_record?

On a Content model have an attribute named slug. When creating a new record, I want to use a helper to populate this field, but on an existing record I want to use the value from the database. Currently I have: <% if @content.new_record? %> <%=…
jyoseph
  • 5,435
  • 9
  • 45
  • 64
17
votes
3 answers

Is there a way to use a Ruby loop inside of HAML's :javascript region?

Inside of HAML, can we have a loop inside the :javascript region? This will work: - 10.upto(20) do |i| :javascript document.getElementById('aDiv').innerHTML += '#{i}'; and this will not: :javascript - 10.upto(20) do |i| …
nonopolarity
  • 146,324
  • 131
  • 460
  • 740
17
votes
1 answer

Rubocop in html.erb files?

I was curious if there was a way to make Rubocop lint/stylecop html.erb files? I realize that the html would make it hard to style cop the embedded Ruby. Has anyone been able to get Rubocop to do this? If not, is there an equivalent tool for this…
Mike
  • 341
  • 1
  • 4
  • 9
17
votes
4 answers

ActionMailer pass local variables to the erb template

I know I could define instance variables e.g: def user_register(username, email) @username = username @email = email mail(:to => email, :subject => "Welcome!", :template_name => "reg_#{I18n.locale}") end But, is there a way to use local…
Dmitri
  • 2,451
  • 6
  • 35
  • 55
16
votes
3 answers

How do I get escape_javascript and other helpers in my sprockets pre-processed js file (not a view)?

I'm using Rails 3.1 and the sprockets stuff. I want to use ERB to pre-process a js file that will then be included using javascript_include_tag. It is generated from code, and so I'm pre-processing it with ERB, but I can't get to the helpers like…
nocache
  • 1,805
  • 16
  • 18
16
votes
4 answers

Rails 3: How to render ERb template in rake task?

I am rendering a pretty huge sitemap HTML file with rake. Unfortunately, the code breaks when I migrate to rails 3. My current code looks like this: @controller = ActionController::Base.new @controller.request =…
Jan
  • 3,044
  • 3
  • 20
  • 32