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
16
votes
1 answer

rails database.yml not accepting ERB

In my database.yml, I have: staging: adapter: <%= ENV['DATABASE_ADAPTER'] %> encoding: <%= ENV['DATABASE_ENCODING'] %> database: <%= ENV['DATABASE'] %> host: <%= ENV['DATABASE_HOST'] %> port: <%= ENV['DATABASE_PORT'].to_i %> pool: <%=…
16
votes
5 answers

yield in ERB without rails

How can I use yield for template inheritance in erb? I want to use erb in a plain ruby CGI script and want to use a base template and subtemplate like it Rails with the application template does.
xaver23
  • 1,301
  • 3
  • 12
  • 14
15
votes
4 answers

Problem using OpenStruct with ERB

EDIT: forgot to include my environment info... Win7x64, RubyInstaller Ruby v1.9.1-p378 EDIT 2: just updated to v1.9.1, patch 429, and still getting this same error. Edit 3: running this same code in Ruby v1.8.7, patch 249, works fine. so it's v1.9.1…
Derick Bailey
  • 72,004
  • 22
  • 206
  • 219
15
votes
2 answers

How do I include HTML in a JS Rails response?

I have a FooController that responds to HTML and JS (AJAX) queries: # app/controllers/foo_controller.rb: class FooController < ApplicationController layout 'foo' def bar respond_to do |format| format.html # foo/bar.html.erb …
James A. Rosen
  • 64,193
  • 61
  • 179
  • 261
15
votes
3 answers

Using Markdown with Rails

I want users to type in Markdown text in a textarea and when they post it, I display the corresponding html. I read that Rails used to have a markdown method or similarly called method, that you could just call on that field in the ERB file: <%=…
at.
  • 50,922
  • 104
  • 292
  • 461
15
votes
3 answers

How to test code coverage for Rails ERB templates?

I'm just building a test suite for a legacy Rails app. The simplecov gem has been great for finding dark corners of the app which need test coverage (or which may be completely unused and OK to remove). I'm invoking simplecov simply by including the…
Alex D
  • 29,755
  • 7
  • 80
  • 126
15
votes
2 answers

echo if variable is defined in ruby/erb

Possible Duplicate: Checking if a variable is defined in Ruby using Tilt's template render method, I pass in #... t setup ... t.render(self, { :a => 'test', :b => 'again' }) in my template.erb <%= a %> <%= b %> say I remove :b from the hash…
tester
  • 22,441
  • 25
  • 88
  • 128
15
votes
2 answers

Passing argument to '.js.erb' template

I want to pass some arguments to the my Javascript template in Rails3 application What I try with respond_to block is: respond_to do |format| format.js({:id=>params[:id]}) end I also tried: respond_to do |format| …
Nitish Upreti
  • 6,312
  • 9
  • 50
  • 92
14
votes
3 answers

Pass Rails JSON variable to javascript variable

I have a Global JSON variable in my Ruby on Rails controller. It looks like this @rails_side_json = { :name => 'Will', :age => 23 }.to_json I want this to be assigned to my javascript variable (var javascript_side_json) which resides in my…
user3422637
  • 3,967
  • 17
  • 49
  • 72
13
votes
2 answers

Rails 3.2 - haml vs. erb. Is haml faster? (february 2012)

I am working on a project and I am still thinking about using HAML (beautiful code, less size of view files) instead of classic ERB template. My worries why I didn't do it yet are the speed of generating views - I read an articles/benchmarks and…
user984621
  • 46,344
  • 73
  • 224
  • 412
13
votes
3 answers

Conditionally set CSS class

How to convert this ERB code:
> into Haml code?
Pewh Gosh
  • 1,031
  • 1
  • 11
  • 29
13
votes
2 answers

Rendering js from a Rails controller with webpacker

Just replaced the js pipeline in my Rails app with webpacker. Most things work correctly, however controllers that render js no longer work as expected. def action format.js { render "javascript_partial" } end Normally, the above would execute a…
sambecker
  • 1,099
  • 12
  • 28
13
votes
3 answers

What's the difference between <% code %> and <%= code %> in Rails erb?

There seems to be a difference between the two, Though I can't tell what exactly. <% code %> And <%= code %>
Ariel
  • 2,638
  • 4
  • 23
  • 27
13
votes
7 answers

Vim html.erb snippets?? snipMate Need a vim tip

When I'm in an html.erb file, I get no snipMate snippets. I would like both HTML and Ruby, or just HTML would be fine, How would I do this? Would I need to write a set of snippets? If so, is there a way of pulling in existing snippets without…
Daniel Upton
  • 5,561
  • 8
  • 41
  • 64
13
votes
6 answers

In Rails, how can I allow some html in a text area?

I have a Rails app (blog) that I am creating. Very basic stuff. In my content area I have a text area for the content of the post. I am needing to include some html in the text area (links, formating, etc). <%= f.text_area :content %> Is there…
Norm
  • 659
  • 3
  • 10
  • 20