Forum: Ruby ERB trim_mode '<>' is not working as expected.

249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2014-03-16 16:33
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 ?

Expected :

<html>
  <head>
    <title> hello </title>
  </head>
  <body>
    <ul>
              <li> dog </li>
              <li> cat </li>
              <li> monkey </li>
        </body>
</html>
2ffac40f8a985a2b2749244b8a1c4161?d=identicon&s=25 Mike Stok (Guest)
on 2014-03-16 18:43
(Received via mailing list)
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).

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

--

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

The "`Stok' disclaimers" apply.
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2014-03-16 20:13
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!
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.