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
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
43
votes
3 answers

Passing parameters to erb view

I'm trying to pass parameters to an erb view using Ruby and Sinatra. For example, I can do: get '/hello/:name' do "Hello #{params[:name]}!" end How do I pass :name to the view? get '/hello/:name' do erb :hello end And how do I read the…
Fábio Perez
  • 23,850
  • 22
  • 76
  • 100
41
votes
6 answers

Rails: access controller instance variable in CoffeeScript or JavaScript asset file

In Rails 3.1 it is not possible to access controller instance variables in an asset js.erb or coffee.erb file using syntax such as <%= @foo %>, where @foo is set in the controller. So then the question is what are the best ways for passing…
Safa Alai
  • 1,223
  • 1
  • 15
  • 27
41
votes
3 answers

Disable HTML escaping in erb templates

In a Rails 3 application I have a domain class where one attribute stores pure HTML content (it's a blog app, the domain class is Post). In the ERB templates, I need to display the content of the attribute as it was formmated, with the HTML tags in…
Lucas
  • 3,059
  • 5
  • 33
  • 48
41
votes
2 answers

Rails button_to - applying css class to button

This is silly, but I just can't figure out how to style the input element created by Rails button_to -- and yes, I've read the docs over and over again and tried the samples. Here's my ERB code: <%= button_to "Claim", action: "claim", idea_id:…
ezuk
  • 3,096
  • 3
  • 30
  • 41
40
votes
6 answers

How do I execute ruby template files (ERB) without a web server from command line?

I need ERB (Ruby's templating system) for templating of non-HTML files. (Instead, I want to use it for source files such as .java, .cs, ...) How do I "execute" Ruby templates from command line?
ivan_ivanovich_ivanoff
  • 19,113
  • 27
  • 81
  • 100
40
votes
3 answers

Loop in Ruby on Rails html.erb file

everybody I'm brand new with Ruby on Rails and I need to understand something. I have an instance variable (@users) and I need to loop over it inside an html.erb file a limitated number of times. I already used this: <% @users.each do |users| %> …
Stark_kids
  • 507
  • 1
  • 5
  • 17
35
votes
6 answers

How do I check if a variable is defined in rails?

<% if dashboard_pane_counter.remainder(3) == 0 %> do something <% end> If dasboard_pane_counter wasn't defined, how can I get this to evaluate to false rather than throw an exception?
cjm2671
  • 18,348
  • 31
  • 102
  • 161
35
votes
3 answers

Rails if statement syntax

I've written the following ERB and am getting a syntax error at the question mark. This helper function from devise currently evaluates as false. What have I missed? <%= if user_signed_in? %> <%= render 'form' %> <%= end %>
cjm2671
  • 18,348
  • 31
  • 102
  • 161
33
votes
4 answers

How to create fixtures (for a Devise user) as a yml.erb in rails (4.1.5)?

Update 3: It seems like this is specific to fixtures in a .yml.erb - even if I have no templated code, it seems like fixtures in a yml.erb file doesn't get loaded. Having a plain .yml file works. This likely has nothing to do with devise per…
Anand
  • 3,690
  • 4
  • 33
  • 64
33
votes
4 answers

Including one erb file into another

I'm writing a command-line tool that will ultimately output an HTML report. The tool is written in Ruby. (I am not using Rails). I'm trying to keep the logic of the application in one set of files, and the HTML templates (the .erb files) in another…
Chris Allen Lane
  • 6,334
  • 5
  • 25
  • 31
32
votes
12 answers

Leaflet Marker not found production env

I got a problem with leaflet. Everything is working fine in development, but in production, my app isn't able to locate the marker-icon.png and marker-shadow.png images. It is looking for the path assets/station/images/marker-icon.png Leaflet js is…
maluss
  • 602
  • 1
  • 8
  • 14
32
votes
5 answers

Handling JSON in JS/ERB template in Rails 3

I have no trouble making typical AJAX calls to and from Rails(3) with JSON objects and jQuery-rails (jQuery library plus a special rails.js file). In one controller, though, I want to RETURN some JSON in an erb template (create.js.erb) after an AJAX…
Michael Waxman
  • 1,815
  • 3
  • 18
  • 32
31
votes
3 answers

How do I run multiple lines of Ruby in html.erb file

I'm using Ruby on Rails and need to run a block of Ruby code in one of my html.erb files. Do I do it like this: <% def name %> <% name = username %> <%= name %> or like this: <% def name name = username %> <%= name %> Thanks for reading.
ben
  • 29,229
  • 42
  • 124
  • 179
30
votes
3 answers

Print in ERB without <%=?

Sometimes it's more convenient to print in <%%>. How to do it in Rails?
Cheng
  • 4,816
  • 4
  • 41
  • 44