ERB trim_mode '<>' is not working as expected

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

<html>
  <head>
    <title> hello </title>
  </head>
  <body>
    <ul>
      <% @animals.each do |item| %>
        <li> <%= item %> </li>
      <% end %>
  </body>
</html>

#output

<html>
  <head>
    <title> hello </title>
  </head>
  <body>
    <ul>

        <li> dog </li>

        <li> cat </li>

        <li> monkey </li>

  </body>
</html>

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 ?

···

--
Posted via http://www.ruby-forum.com/.

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 %>:

<html>
<head>
   <title> hello </title>
</head>
<body>
   <ul>
<% @animals.each do |item| %>
       <li> <%= item %> </li>
<% end %>
</body>
</html>

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 </ul> tag missing too...

Hope this helps,

Mike

···

On Mar 16, 2014, at 3:33 PM, Arup Rakshit <lists@ruby-forum.com> wrote:

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

<html>
<head>
   <title> hello </title>
</head>
<body>
   <ul>
     <% @animals.each do |item| %>
       <li> <%= item %> </li>
     <% end %>
</body>
</html>

#output

<html>
<head>
   <title> hello </title>
</head>
<body>
   <ul>

       <li> dog </li>

       <li> cat </li>

       <li> monkey </li>

</body>
</html>

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 ?

--

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.

Mike Stok wrote in post #1140057:

···

On Mar 16, 2014, at 3:33 PM, Arup Rakshit <lists@ruby-forum.com> wrote:

   erb.result(binding)

</html>
       <li> dog </li>

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!

--
Posted via http://www.ruby-forum.com/\.