【问题标题】:On octobercms static pages plugin render a group of fields with a repeater inside在 octobercms 静态页面插件上渲染一组带有中继器的字段
【发布时间】:2018-05-15 20:42:47
【问题描述】:

在“content/static-pages/index.htm”上,我可以看到可以在页面上呈现的已保存字段:

然后在局部我可以使用 {{ data.section_color }} 来呈现一个字段。

现在我想在“meta/groups.yaml”中创建一个转发器字段,如下所示:

carousel2:
  name: Carousel2
  description: Carousel2
  icon: icon-file-image-o
  fields:
    section_carousel2:
       type: repeater
       prompt: Add new subitem
       form:
          fields:
            section_carouselimage2:
              label: Image2
              type: mediafinder
              mode: image
            section_carouseltitle2:
              label: Title2
              type: text
            section_carouselsubtitle2:
              label: Subtitle2
              type: textarea
              size: small

我可以在后端看到该字段,并且可以使用它保存数据。现在我在“content/static-pages/index.htm”上有:

[viewBag]
title = "Home"
url = "/"
layout = "static"
is_hidden = 0
navigation_hidden = 0
sections[0][section_carousel2][1][section_carouselimage2] = "/carousel/bg-1.jpg"
sections[0][section_carousel2][1][section_carouseltitle2] = "Carousel2 Title"
sections[0][section_carousel2][1][section_carouselsubtitle2] = "Carousel2 Subitle"
sections[0][section_carousel2][2][section_carouselimage2] = "/carousel/bg-2.jpg"
sections[0][section_carousel2][2][section_carouseltitle2] = "Carousel2 Title2"
sections[0][section_carousel2][2][section_carouselsubtitle2] = "Carousel2 Subtitle2"
sections[0][_group] = "carousel2"
==

问题是我找不到渲染这个字段的方法。如何在转发器组内呈现转发器字段?如何呈现例如“section_title2”字段?

【问题讨论】:

    标签: octobercms octobercms-plugins octobercms-backend octobercms-viewbag


    【解决方案1】:

    嗯,以免假设您正在添加 carousel2

    好的,我们在 meta/groups.yaml 中添加了它的标记,所以基本上我们正在制作 carousel2 组。

    所以现在解决方案,我们如何显示它的信息/数据。

    首先,我们需要在 cms blocks/carousel2 中创建部分内容,以便将 carousel2 放置在其他部分内容所在的 blocks 文件夹中(简单/反应/等等...)。

    好的,现在我们需要在里面添加这个内容

    <section>
      <!-- render inner section -->
        {% for slide in data.section_carousel2 %}
            <!-- this block will repeat / based on added slides( so probably slider markup will be here) -->
            <p>{{ slide.section_carouselimage2 }}</p>
            <p>{{ slide.section_carouseltitle2 }}</p>
            <p>{{ slide.section_carouselsubtitle2 }}</p>        
        {% endfor %}
    </section>
    

    现在您可以访问 for 循环中的所有字段。我们使用循环是因为您的组是中继器。

    让我们稍微理解一下。


    data : = 这个变量被传递给每个部分。它关于您的组的数据,在我们的例子中是 carousel2

    现在数据有 2 件事 第一:组名,第二我们的真实数据我们保存在页面中。

    现在我们可以使用 data.section_carousel2 来获取保存的数据,因为我们知道它的中继器类型,因此要获取我们需要添加 for 循环的数据。

    现在在 for 循环中,我们使用了 slide 变量,该变量将为每次迭代获取重复器内所有字段的所有数据。

    所以现在在这个循环中,您拥有我们在 meta/groups.yaml 文件中声明的字段 slide.section_carouselimage2。

    如果有什么不明白或不起作用,请发表评论。

    【讨论】:

    • 很好的解释。我错过了“数据”。在for循环里面。现在正在工作。谢谢。
    猜你喜欢
    • 2021-01-13
    • 2018-05-24
    • 2021-11-03
    • 2017-12-16
    • 1970-01-01
    • 1970-01-01
    • 2018-09-30
    • 2022-11-09
    • 1970-01-01
    相关资源
    最近更新 更多