rtomayko / tilt

Generic interface to multiple Ruby template engines

Home Page:http://github.com/rtomayko/tilt

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to indent in erb?

Pistos opened this issue · comments

Thought maybe this project would be interested in a bug report in Hanami that possibly might be a bug in Tilt. I'm copying a snippet of the discussion below, but see hanami/hanami#909 for full details.

❯ cat bar.html.erb
true is...
  <% case true %>
  <% when true %>
    true.
  <% else %>
    false! (wait, what?)
  <% end %>

❯ ruby -rtilt -rerb -e "puts Tilt.new('bar.html.erb').render"
bar.html.erb:7: warning: else without rescue is useless
/Users/kaikuchn/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/tilt-2.0.8/lib/tilt/template.rb:274:in `class_eval': bar.html.erb:2: syntax error, unexpected tIDENTIFIER, expecting keyword_when (SyntaxError)
cat "  ";  case true ; _erbout.concat "\n"
                              ^
bar.html.erb:3: syntax error, unexpected keyword_when, expecting keyword_end
; _erbout.concat "  ";  when true ; _erbout.concat "\n"
                            ^
bar.html.erb:9: syntax error, unexpected keyword_ensure, expecting keyword_end
        ensure
              ^
bar.html.erb:13: syntax error, unexpected keyword_end, expecting end-of-input
end;end;end;end
           ^
	from /Users/kaikuchn/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/tilt-2.0.8/lib/tilt/template.rb:274:in `compile_template_method'
	from /Users/kaikuchn/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/tilt-2.0.8/lib/tilt/template.rb:236:in `block in compiled_method'
	from /Users/kaikuchn/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/tilt-2.0.8/lib/tilt/template.rb:235:in `synchronize'
	from /Users/kaikuchn/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/tilt-2.0.8/lib/tilt/template.rb:235:in `compiled_method'
	from /Users/kaikuchn/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/tilt-2.0.8/lib/tilt/template.rb:169:in `evaluate'
	from /Users/kaikuchn/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/tilt-2.0.8/lib/tilt/template.rb:109:in `render'
	from -e:1:in `<main>'

I can't reproduce it here:

$ cat bar.html.erb
true is...
  <% case true %>
  <% when true %>
    true.
  <% else %>
    false! (wait, what?)
  <% end %>
$ ruby -rtilt -rerb -e "puts Tilt.new('bar.html.erb').render"
true is...
    true.

This essentially depends on how the ERB-template handles whitespace. If the whitespace between case and when is ignored, then it will compile as expected. Otherwise it inserts a buf.concat(" ") to insert the space. The easiest way to force it to be valid is to do:

<% case foo; when first_case %>
…
<% when other_case %>
…
<% end %>

@judofyr I don't really follow. How does one control whether the problem occurs or not, given the initial example code? You said you couldn't reproduce, but I don't understand the difference between your attempt and ours.

As mentioned in the original hanami issue, we can work around the problem like this:

true is...
  <%
  case true
  when true
  %>
    true.
  <% else %>
    false! (wait, what?)
  <% end %>