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

拟墨画扇 提交于 2019-12-17 16:28:02

问题


I'm using "Button dropdowns" from Twitter Bootstrap with HAML. In the Bootstrap docs I have found the example:

<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>

I have tried rewrite it using 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        

But got the error message:

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

So Bootstrap said me put the element inside -tag, but HAML didn't allow to do it. How can I fix the problem?


回答1:


The problem is in these lines:

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

The error message: content can't be both given on the same line as %a and nested within it refers to Action which is content “on the same line as %a”, and %span{:class => 'caret'}, which is content “nested within it”.

More generally, and perhaps easier to see, you can’t have something like this:

%tag Some content here
  And something here as well

The fix is to nest both under the %a:

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

This gives the desired output:

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



回答2:


Don't forget your Haml idioms!

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

is equivalent to:

.btn-group.task_controller

…etc.




回答3:


Try this:

%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  

Tag name and content can be on the same line only when there are no more content lines in the tag.



来源:https://stackoverflow.com/questions/10202931/hamlsyntaxerror-illegal-nesting-content-cant-be-both-given-on-the-same-lin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!