5

I have been going through a crash course of Ruby and Ruby on Rails and i can't figure this out: In embedded ruby html files, there are several tags. <% %> for execution <%= %> for output, but what function do these tags serve: <%= -%>, what's with the "-" sign at the end ?

Thanks.

Xeperis
  • 1,397
  • 2
  • 20
  • 38

2 Answers2

2

This link contains a good overview of erb markup.

From the site:

RECOGNIZED TAGS

ERB recognizes certain tags in the provided template and converts
them based on the rules below:

<% Ruby code -- inline with output %>

<%= Ruby expression -- replace with result %>

<%# comment -- ignored -- useful in testing %>

% a line of Ruby code -- treated as <% line %> (optional -- see ERB.new)

%% replaced with % if first thing on a line and % processing is used

<%% or %%> -- replace with <% or %> respectively

All other text is passed through ERB filtering unchanged.

jkeuhlen
  • 4,091
  • 20
  • 35
ksol
  • 10,940
  • 5
  • 35
  • 63
  • 3
    Links is dead. An archived version: http://wayback.archive.org/web/20100215150502/http://cheat.errtheblog.com/s/erb/ – Miscreant Feb 20 '13 at 16:19
1

In ruby document there is not the details about this usage <%- or -%>. But it works well by this:

erbA = ERB.new(erbA_str, 1, '-', "@output_buffer")

Notice the 3rd parameter '-' ! It removes the pre or post blanks when using <%- and -%> accordingly.

Nat Ritmeyer
  • 5,380
  • 8
  • 39
  • 54
fantaxy025025
  • 799
  • 7
  • 6