如何关闭容器在循环中?
我有一个产品列表,我想在产品 Feed 中展示广告。
我想要这样的东西:
<div id="container">
<div id="product">Bla..</div>
<div id="product">Bla..</div>
<div id="product">Bla..</div>
</div>
<div id="add">
Adsense Stuff
</div>
<div id="container">
<div id="product">Bla..</div>
<div id="product">Bla..</div>
<div id="product">Bla..</div>
<div id="product">Bla..</div>
<div id="product">Bla..</div>
</div>
在 ERB 中,我会:
<div id="container">
<% productes.each_with_index do |product,index| %>
<div id="product"><%= product %></div>
<% if index == 2 %>
</div>
<div id="add">
Adsense Stuff
</div>
<div id="container">
<% end %>
<% end %>
</div>
你如何将其翻译为 Haml 或 Slim?
我不想将循环分成两个循环,原因有两个:我不知道产品按页面计数,并且我有一些更复杂的代码,它们使用与 Rails cycle()
cycle() 帮助程序。因此,找到一个使之成为可能的技巧将对我有很大帮助。
I have a list of products and I want to show an ad in the product feed.
I want something like:
<div id="container">
<div id="product">Bla..</div>
<div id="product">Bla..</div>
<div id="product">Bla..</div>
</div>
<div id="add">
Adsense Stuff
</div>
<div id="container">
<div id="product">Bla..</div>
<div id="product">Bla..</div>
<div id="product">Bla..</div>
<div id="product">Bla..</div>
<div id="product">Bla..</div>
</div>
In ERB, I would have:
<div id="container">
<% productes.each_with_index do |product,index| %>
<div id="product"><%= product %></div>
<% if index == 2 %>
</div>
<div id="add">
Adsense Stuff
</div>
<div id="container">
<% end %>
<% end %>
</div>
How you translate this to Haml or Slim?
I don't want to break the loop in two loops for two reasons: I don't know the products count by page and I have some more elaborate code which uses the same HTML tricks with the Rails cycle()
helper. So, it will help me a lot to find a trick to make it possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Haml 允许您在需要时编写原始 HTML 作为输出。虽然很奇怪,但您可以使用它来实现您的目标,就像您对 Erb 所做的那样:
缩进看起来很奇怪,但您可以看到有两个容器,其中一个容器外面都装有 AdSense 内容。
Haml lets you write raw HTML as output when you want it. Although weird, you can use this to achieve your goals here, as you did with Erb:
The indentation looks weird, but you can see that you have two containers with the AdSense stuff outside either.
在 HAML 中,
我不会理会容器,只需设置 css 来处理产品和广告类。 (这也表明您有多个同名的 id,这些应该更改为类)。
In HAML
I wouldn't bother with the container, just set up the css to deal with the product and ad classes. (Which also brings up the fact that you have multiple ids of the same name, these should be changed to classes).
这个应该可以吧?
This should do it?
可能的 Haml 解决方案,使用
surround
helper:这有点像 hack,因为我们“假装”关闭和打开容器 div;据哈姆所知,我们仍然深陷其中。出于这个原因,它还引入了一些重复,因为您必须在两个位置指定“容器”类(以及 div 可能具有的任何其他属性)。
此解决方案类似于 @Phrogz 的解决方案,但它更“Haml-ly”并允许您使用 Haml 语法定义添加 div。
A possible Haml solution, using the
surround
helper:This is a bit of a hack, in that we're "faking" closing and opening the container div; as far as Haml knows we're still in it. For this reason it also introduces a bit of repetition in that you have to specify the "container" class (and any other attributes that div might have) in two places.
This solution is similar to @Phrogz's solution but this is a bit more "Haml-ly" and allows you to use Haml syntax to define the add div.