Haml到Html,如何为父标签分配正确的值?

时间:2012-10-20 17:58:21

标签: html ruby-on-rails haml

我是网络开发新手。下面我有haml的代码

%th
    %a#title_header
        %a{:th => ("hilite" if @sort == "title")}= link_to 'Movie Title', :sort => "title"

给了我以下HTML

<th>
   <a id='title_header'>
       <a th='hilite'><a href="/movies?sort=title">Movie Title</a></a>
   </a>
</th>

虽然我期待得到的是

<th class='hilite'>
    <a id='title_header'><a href="/movies?sort=title">Movie Title</a></a>
</th>

2 个答案:

答案 0 :(得分:1)

%th.hilite
  %a#title_header
    %a{:href => "/movies?sort=title"} Movie Title

顺便提一下,有一个名为http://html2haml.heroku.com/的项目,请查看它!

答案 1 :(得分:1)

这与我的答案非常相似:Adding dynamic attributes to HAML tag using helper method in rails

在你的情况下它应该是:

%th{:class => if @sort == 'title' then 'hilite' end}
    %a#title_header
        %a= link_to 'Movie Title', :sort => "title