Haml :: SyntaxError - 非法嵌套:内容不能与%a在同一行上给出并嵌套在其中

时间:2012-04-18 04:47:04

标签: haml twitter-bootstrap

我正在使用来自Twitter Bootstrap和HAML的“按钮下拉列表”。在Bootstrap文档中,我找到了示例:

<div class="btn-group">
  <a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
    Action
    <span class="caret"></span>
  </a>
  <ul class="dropdown-menu">
    <!-- dropdown menu links -->
  </ul>
</div>

我尝试使用HAML重写它:

%div{:class => 'btn-group task_controller'}
  %a{:class => 'btn-mini dropdown-toggle', "data-toggle" => 'dropdown', :href => '#'} Action
    %span{:class => 'caret'}
  %ul{:class => 'dropdown-menu'}
    %a{:class => 'close_task', :name => task.name, :href => '#' } Close        

但收到了错误消息:

Haml::SyntaxError - Illegal nesting: content can't be both given on the same line as %a and nested within it.

所以Bootstrap说我把元素放在-tag中,但是HAML不允许这样做。我该如何解决这个问题?

3 个答案:

答案 0 :(得分:31)

问题在于以下几点:

%a{:class => 'btn-mini dropdown-toggle', "data-toggle" => 'dropdown', :href => '#'} Action
  %span{:class => 'caret'}

错误消息:content can't be both given on the same line as %a and nested within it是指Action的内容“与%a在同一行”,而%span{:class => 'caret'}是内容“嵌套在其中”。

更一般地说,也许更容易看到,你不能有这样的东西:

%tag Some content here
  And something here as well

修复方法是在%a

下嵌套
%a{:class => 'btn-mini dropdown-toggle', "data-toggle" => 'dropdown', :href => '#'}
  Action
  %span{:class => 'caret'}

这给出了所需的输出:

<a class='btn-mini dropdown-toggle' data-toggle='dropdown' href='#'>
  Action
  <span class='caret'></span>
</a>

答案 1 :(得分:7)

不要忘记你的汉语成语!

#div{:class => 'btn-group task_controller'}

相当于:

.btn-group.task_controller

...等

答案 2 :(得分:0)

试试这个:

%div{:class => 'btn-group task_controller'}
  %a{:class => 'btn-mini dropdown-toggle', "data-toggle" => 'dropdown', :href => '#'}
    Action
    %span{:class => 'caret'}
  %ul{:class => 'dropdown-menu'}
    %a{:class => 'close_task', :name => task.name, :href => '#' } Close  

只有当标签中没有更多内容行时,标签名称和内容才能在同一行。