如何关闭容器
在循环中?

发布于 2024-12-27 13:27:46 字数 1197 浏览 2 评论 0原文

我有一个产品列表,我想在产品 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

恍梦境° 2025-01-03 13:27:46

Haml 允许您在需要时编写原始 HTML 作为输出。虽然很奇怪,但您可以使用它来实现您的目标,就像您对 Erb 所做的那样:

TEMPLATE = '
.container
  - products.each_with_index do |product,index|
    - if index == 2
      </div>
      <div class="ad">AdSense Stuff</div>
      <div class="container">
    .product<
      = product
'

require 'haml'
products = %w[ cat box kitten shoes hounds ]
puts Haml::Engine.new(TEMPLATE).render binding

#=> <div class='container'>
#=>   <div class='product'>cat</div>
#=>   <div class='product'>box</div>
#=>   </div>
#=>   <div class="ad">AdSense Stuff</div>
#=>   <div class="container">
#=>   <div class='product'>kitten</div>
#=>   <div class='product'>shoes</div>
#=>   <div class='product'>hounds</div>
#=> </div>

缩进看起来很奇怪,但您可以看到有两个容器,其中一个容器外面都装有 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:

TEMPLATE = '
.container
  - products.each_with_index do |product,index|
    - if index == 2
      </div>
      <div class="ad">AdSense Stuff</div>
      <div class="container">
    .product<
      = product
'

require 'haml'
products = %w[ cat box kitten shoes hounds ]
puts Haml::Engine.new(TEMPLATE).render binding

#=> <div class='container'>
#=>   <div class='product'>cat</div>
#=>   <div class='product'>box</div>
#=>   </div>
#=>   <div class="ad">AdSense Stuff</div>
#=>   <div class="container">
#=>   <div class='product'>kitten</div>
#=>   <div class='product'>shoes</div>
#=>   <div class='product'>hounds</div>
#=> </div>

The indentation looks weird, but you can see that you have two containers with the AdSense stuff outside either.

三寸金莲 2025-01-03 13:27:46

在 HAML 中,

-products.each_with_index do |product,index|
  .product= product
  -if index == 2
    .ad= Adsense Stuff

我不会理会容器,只需设置 css 来处理产品和广告类。 (这也表明您有多个同名的 id,这些应该更改为类)。

In HAML

-products.each_with_index do |product,index|
  .product= product
  -if index == 2
    .ad= Adsense Stuff

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

沉睡月亮 2025-01-03 13:27:46
- products.each_with_index do |product,index|
  .product
    = product
    - if index == 2
      .ad= Adsense Stuff

这个应该可以吧?

- products.each_with_index do |product,index|
  .product
    = product
    - if index == 2
      .ad= Adsense Stuff

This should do it?

手长情犹 2025-01-03 13:27:46

可能的 Haml 解决方案,使用 surround helper

.container
  -products.each_with_index do |product,index|
    .product=product
    -if index == 2
      =surround "</div>", "<div class='container'>" do
        .add
          Adsense stuff

这有点像 hack,因为我们“假装”关闭和打开容器 div;据哈姆所知,我们仍然深陷其中。出于这个原因,它还引入了一些重复,因为您必须在两个位置指定“容器”类(以及 div 可能具有的任何其他属性)。

此解决方案类似于 @Phrogz 的解决方案,但它更“Haml-ly”并允许您使用 Haml 语法定义添加 div。

A possible Haml solution, using the surround helper:

.container
  -products.each_with_index do |product,index|
    .product=product
    -if index == 2
      =surround "</div>", "<div class='container'>" do
        .add
          Adsense stuff

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文