11

我开始使用 HAML,并且正在转换我的第一个文件。表面上正确的省略“- end”:

- if current_user
= link_to 'Edit Profile', edit_user_path(current_user.id)
= link_to 'Logout', logout_path
- else
= link_to 'Register', new_user_path
= link_to 'Login', login_path

得到我:

app/views/layouts/application.html.haml:28: syntax error, unexpected kENSURE, expecting kEND
app/views/layouts/application.html.haml:30: syntax error, unexpected $end, expecting kEND

虽然合乎逻辑

- if current_user
= link_to 'Edit Profile', edit_user_path(current_user.id)
= link_to 'Logout', logout_path
- else
= link_to 'Register', new_user_path
= link_to 'Login', login_path
- end

得到我:

You don't need to use "- end" in Haml. Use indentation instead:
- if foo?
  %strong Foo!
- else
  Not foo.

如何让这个条件语句在 HAML 中工作?

4

3 回答 3

21

HAML 是基于缩进的,解析器可能很棘手。代替

- if current_user
= link_to 'Edit Profile', edit_user_path(current_user.id)
= link_to 'Logout', logout_path
- else
= link_to 'Register', new_user_path
= link_to 'Login', login_path

- if current_user
  = link_to 'Edit Profile', edit_user_path(current_user.id)
  = link_to 'Logout', logout_path
- else
  = link_to 'Register', new_user_path
  = link_to 'Login', login_path

试一试。请注意 link_to 行上的缩进是如何变化的。

于 2009-07-28T03:32:17.757 回答
3
- if current_user
  = link_to 'Edit Profile', edit_user_path(current_user.id)
  = link_to 'Logout', logout_path
- else
  = link_to 'Register', new_user_path
  = link_to 'Login', login_path
于 2009-07-28T08:15:45.327 回答
1

有关 haml 文件的更多信息

http://rubyonrails-tutor.blogspot.com/2010/02/hamltutorial-for-rails-developers.html

于 2010-02-24T12:06:14.947 回答