Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unwanted whitespace in Erb view

Tags:

ruby

erb

sinatra

Here's my sinatra code:

get '/' do
    foo = 'not bar'
    erb :index
end

My layout.erb

<html>
  <head></head>
  <body>
    <%= yield %>
  </body>
</html>

My index.erb

<div class="container">
</div>

now the problem is screen shot of problem

The extra text (hilighted with yellow) disturbs my design Any idea why this is happening? this dosn't happen if I dont use layout and use only index.erb with all html code

[Edit:] Use of <%= yield -%> throws error (unexpected tUMINUS, expecting kEND ; @_out_buf.concat " "; - yield -; @_out_buf.concat "\n" ) in .... layout.rb

like image 779
goutham Avatar asked Feb 24 '23 06:02

goutham


2 Answers

my best guess is the 4 spaces come from the soft tabs in your layout.erb

<body>
____<%= yield %>
</body>

try <body><%= yield%></body>?

I've been using Slim a long while and

body
= yield

never fails me whitespace

hate ERB

like image 82
han Avatar answered Mar 11 '23 09:03

han


You can set this up with *trim_mode* parameter for ERB

From http://ruby-doc.org/stdlib-1.9.3/libdoc/erb/rdoc/ERB.html#method-c-new :

If trim_mode is passed a String containing one or more of the following modifiers, ERB will adjust its code generation as listed:

%  enables Ruby code processing for lines beginning with %
<> omit newline for lines starting with <% and ending in %>
>  omit newline for lines ending in %>
like image 37
Mailo Světel Avatar answered Mar 11 '23 08:03

Mailo Světel