有条件地关闭 HAML 中的标签
我正在迭代一组项目并将它们显示在嵌套在 div 中的列表中。目标是每天都有一个 div,并在每个 div 中显示当天的项目。
我该如何在 HAML 中执行此操作?我不认为我可以(或应该)有条件地关闭并创建一个新标签,就像我在 erb.
我尝试过:
- @items.each do |item|
- if item date is diff from previous make a new container
.container
%h2 #{item.date}
= yield_content :display_item item
- else
= yield_content :display_item item
但这会产生以下结果:
<div class="container">
<h2>01/28/2012</h2>
<ul>
<li>
... item
</li>
</ul>
</div>
<li>
...item
</li>
但我想要同一 div 中的其他项目。 我正在使用 ruby、sinatra(包括 content_for 助手)
I'm iterating through a set of items and displaying them in lists nested in divs. The goal is to have a div for each day and within each div show the items for that day.
How do I do this in HAML? I don't think I can (or should) conditionally close and create a new tag like I could in erb.
I tried:
- @items.each do |item|
- if item date is diff from previous make a new container
.container
%h2 #{item.date}
= yield_content :display_item item
- else
= yield_content :display_item item
But this creates the following:
<div class="container">
<h2>01/28/2012</h2>
<ul>
<li>
... item
</li>
</ul>
</div>
<li>
...item
</li>
But I want the other item in the same div.
I'm using ruby, sinatra (including the content_for helper)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案是使用更多更好的 Ruby :)
参见
Enumerable#group_by
文档了解更多详细信息。您可以 关闭并重新打开容器和标头,如下所示你可能在想,但这是一个可怕的、难以维护的黑客行为,我建议不要这样做。拥抱Ruby和Haml的优雅;不要像 ERB 那样写 Haml。
The answer is to use more and better Ruby :)
See
Enumerable#group_by
docs for more details.You could close and re-open the containers and headers as you were thinking, but this is a horrible, hard-to-maintain hack that I would suggest against. Embrace the elegance of Ruby and Haml; don't write Haml like it was ERB.