【问题标题】:Render component index in Magnolia template script在 Magnolia 模板脚本中渲染组件索引
【发布时间】:2023-03-16 00:57:01
【问题描述】:

我正在使用 Magnolia 5.6 Enterprise Pro CMS,并且我有一个类型列表区域,其中包含一个可用组件:

areas:
  steps:
    renderType: freemarker
    availableComponents:
      step:
        id: obi-module:components/step

这样我就可以在steps区域添加N个相同类型的组件了。

问题:现在我想在step组件的模板脚本中显示组件索引,但是不知道怎么实现。

在外部组件的模板脚本中,我可以执行以下操作:

[#assign stepList=cmsfn.children(content.steps) ]

...

<p>Number of steps: ${stepList?size!'#'}</p>

...

[#list stepList as component]
    [@cms.component content=component contextAttributes={'index':component?counter} /]
[/#list]

step 组件的模板脚本中,我可以使用 index 变量:

${(ctx.index)!'#'}

这显示了所需的从 1 开始的索引号,但现在在 Magnolia 中我无法再编辑组件,因为我没有为 step 组件获得典型的绿色 Magnolia 编辑器栏。 p>

当我这样放置模板脚本时:

[#assign stepList=cmsfn.children(content.steps) ]

...

<p>Number of steps: ${stepList?size!'#'}</p>

...

[@cms.area name="steps" /]

我可以编辑组件,但我不知道如何获取(列表)索引。

有谁知道如何做到这一点?

【问题讨论】:

  • 我对 Magnolia 的特定部分一无所知,但注意到一些无关的东西。 ${list?size!'#'} 永远不会打印 #。如果您想在缺少list 时这样做,那么您需要${(list?size)!'#'}

标签: java freemarker magnolia


【解决方案1】:

我的错 - 我正在从(外部)步骤组件的脚本中迭代步骤组件,但我必须在区域脚本中进行。所以我将步骤组件定义更改为:

areas:
  steps:
    renderType: freemarker
    templateScript: /my-module/components/steps-area.ftl
    availableComponents:
      step:
        id: my-module:components/step

区域模板脚本(steps-area.ftl)看起来像这样:

[#list components as component]
    [@cms.component content=component contextAttributes={'index':component?counter} /]
[/#list]

【讨论】:

    【解决方案2】:

    这是一个名为 _index 的 freemarker 内置函数……你可以试试这个:

        [#list components as component ]
            Component Number: '${component_index + 1!}'
            [@cms.component content=component /]
        [/#list]
    

    编辑:如果这不起作用,请尝试 component?index 而不是 component_index ... 仅取决于您正在使用的 magnolia 版本的 magnolia 随附的 freemarker 版本。

    【讨论】:

    • 嘿布拉德利,您在(外部)步骤组件的脚本中呈现索引,我通过 contextAttributes 将它传递给内部步骤组件的脚本。但同样,当我迭代组件时,内部步骤组件周围没有绿色 Magnolia 编辑器栏来配置它们。当我使用@cms.area freemarker 指令时,我只有这些绿色条。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    • 1970-01-01
    • 2019-05-17
    • 1970-01-01
    相关资源
    最近更新 更多