【问题标题】:ERB vs HAML conversion of an if condition?if条件的ERB与HAML转换?
【发布时间】:2010-11-14 13:50:08
【问题描述】:

我开始使用 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 中工作?

【问题讨论】:

  • 你在你的问题中有答案...。错误信息告诉你究竟该怎么做。

标签: ruby-on-rails haml erb


【解决方案1】:

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 行上的缩进是如何变化的。

【讨论】:

  • "缩进不一致:缩进使用了 5 个空格,但文档的其余部分缩进使用了 2 个空格。"它不喜欢它。 :(
  • 您缩进的空格太多或太少。你能发布你的整个模板吗?
  • 我又看了一下间距,我认为你是对的。我想我修复了缩进,但现在我得到了这个:/app/models/user_session.rb:5: syntax error, unexpected 'w3.org/1999/xhtml" xml:lang="en" lang="en"> ^ /app/models/user_session. rb:8: 语法错误,意外的 tIDENTIFIER,期待 $end w3.org/1999/xhtml" xml:lang="en" lang="en"> ^ 我错过了什么?
  • 这似乎与 Haml 无关。听起来您不知何故不小心将 HTML 放入了模型类文件中。
  • @Chuck:HAML 不用于模型,它用于视图。 @sleepycat:您的 HAML 文件中有 HTML。请进一步阅读 H​​AML 并找到一些示例。
【解决方案2】:
- 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

【讨论】:

    【解决方案3】:
    猜你喜欢
    • 1970-01-01
    • 2012-10-06
    • 1970-01-01
    • 1970-01-01
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多