Ruby code :
require ‘erb’
class Animal
def add(name)
(@animals ||= []) << name
end
def generate_template
erb = ERB.new(File.read("#{dir}/test.rhtml"),0,’<>’)
erb.result(binding)
end
end
animal = Animal.new
animal.add(‘dog’)
animal.add(‘cat’)
animal.add(‘monkey’)
puts animal.generate_template
#.rhtml file
hello
<% @animals.each do |item| %>
- <%= item %>
<% end %>
#output
hello
<li> dog </li>
<li> cat </li>
<li> monkey </li>
Note : In the output you can see, none of the new line has been
suppressed. but I passed trim_mode as ‘<>’.
I passed '<>'
to suppress the new lines, but I couldn’t. What wrong I
did ?
Expected :
hello
On Mar 16, 2014, at 3:33 PM, Arup R. [email protected] wrote:
erb.result(binding)
dog
I passed '<>'
to suppress the new lines, but I couldn’t. What wrong I
did ?
The documentation for the trim mode ‘<>’ says “omit newline for lines
starting with <% and ending in %>”, and the animals.each line and its
end have leading spaces before the <%, so the line doesn’t start with <%
- it starts with spaces (or some other white space character).
You can either change the template so the lines for which you want to
suppress newlines start and end with the <% and %>:
hello
<% @animals.each do |item| %>
- <%= item %>
<% end %>
Or you can use the trim mode of ‘>’ which lets you have leading spaces
because it checks only for the line ending with %>. This mode will let
you indent your erb “nicely”.
I think there is a
tag missing too…
Hope this helps,
Mike
–
Mike S. [email protected]
http://www.stok.ca/~mike/
The “`Stok’ disclaimers” apply.
Mike S. wrote in post #1140057:
On Mar 16, 2014, at 3:33 PM, Arup R. [email protected] wrote:
erb.result(binding)
dog
I passed '<>'
to suppress the new lines, but I couldn’t. What wrong I
did ?
The documentation for the trim mode ‘<>’ says “omit newline for lines
starting with <% and ending in %>”, and the animals.each line and its
end have leading spaces before the <%, so the line doesn’t start with <%
- it starts with spaces (or some other white space character).
Read the doc wrongly. I thought doc is saying inside <% … %>.
Thank you Mike!