Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails HAML conversion problems - unexpected kENSURE, expecting kEND

I'm trying to switch over to using HAML for my views but I keep getting unexpected KENSURE messages. I've used html2haml to switch over working view files. First run through it told me I didn't need the end that html2haml had in its output so I removed it and now I get errors that look like it is complaining about the form not being ended.

What am I doing wrong?

error message:

compile error
app/views/sessions/new.html.haml:20: syntax error, unexpected kENSURE, expecting kEND
app/views/sessions/new.html.haml:22: syntax error, unexpected $end, expecting kEND

application.html.haml:

!!!
%html
%head
  %title
    = APP_CONFIG[:site_name] + ': ' + @page_title  
  == <meta http-equiv="content-type" content="text/xhtml; charset=utf-8" />
  == <meta http-equiv="imagetoolbar" content="no"  />
  == <meta name="distribution" content="all" />
  == <meta name="robots" content="all"  />
  == <meta name="resource-type" content="document" />
  == <meta name="MSSmartTagsPreventParsing" content="true"   />
  = stylesheet_link_tag 'base'
  = javascript_include_tag :defaults

  %body
    #container
      #header
        - if logged_in?
          = link_to 'Logout', logout_path
        - else
          = link_to 'Login', login_path
          = link_to 'Signup', signup_path
      #content
        = flash_messages
        = yield :layout

and sessions/new.html.haml

= title "Login", :h2
- form_tag session_path do
%fieldset
  %legend
    Your Details
  %ol
    %li
      = label_tag 'login', 'Username'
      = text_field_tag 'login', @login
    %li
      = label_tag 'password'
      = password_field_tag 'password', nil
    %li
      = label_tag 'remember_me', 'Remember me'
      = check_box_tag 'remember_me', '1', @remember_me
.buttons
  = submit_tag 'Login'
  = link_to 'Forgotten Password', forgot_password_path
like image 712
srboisvert Avatar asked Jun 09 '09 17:06

srboisvert


3 Answers

This is something haml does when you've gotten your spacing wrong. html2haml does most of the work for you, but you still have to review the changes. The problem in sessions/new.html.haml is that you are starting a do block with no content:

- form_tag session_path do
%fieldset

The whole fieldset should be nested (indented) within that do block. Because there's no content at all, haml fails to put in an end declaration, so when the file gets interpreted, you get:

unexpected $end, expecting kEND

meaning "I hit the end of the file ($end), I was expecting the end keyword (kEND)"

like image 193
austinfromboston Avatar answered Nov 16 '22 10:11

austinfromboston


As shown, the first chunk has %body indented one level more than %head, and %head at the same level as %html, neither of which is good. If it works, you may get something like

<html>
</html>
<head>
  ...
  <body>
  ...
  </body>
</head>

I think most browsers will choke on that! ;-)

In general I'm not a fan of html2haml, which although it tries hard doesn't seem to be able to handle everything. Unless you have a seriously large pile of view files, I'd be inclined to spend the time reworking the files by hand. You don't have to do them all at once, why not just translate them as you go? You'll learn a lot more about HAML that way, too. Enough that - hopefully - you'll find the whole indentation thing becomes instinctive, after which progress begins to accelerate.

like image 32
Mike Woodhouse Avatar answered Nov 16 '22 09:11

Mike Woodhouse


HAML behaves really poorly with problems like tabs instead of whitespace, and incorrect indentation.

My workaround (at least on OSX using TextMate) is to use TextMate, have the HAML/SASS bundle installed. Then I use CMD-[ to un-indent the text, following by CMD-] to re-indent the text properly. That gets rid of any invisible tabs and incorrect indentation, plus reveals any structural problems.

like image 36
Rob Avatar answered Nov 16 '22 10:11

Rob