Java 在Magnolia模板脚本中渲染组件索引
我使用的是Magnolia 5.6 Enterprise Pro CMS,我有一个类型列表区域,其中有一个可用组件:Java 在Magnolia模板脚本中渲染组件索引,java,freemarker,magnolia,Java,Freemarker,Magnolia,我使用的是Magnolia 5.6 Enterprise Pro CMS,我有一个类型列表区域,其中有一个可用组件: areas: steps: renderType: freemarker availableComponents: step: id: obi-module:components/step 因此,我可以将N个相同类型的组件添加到步骤区域 问题:现在我想在步骤组件的模板脚本中显示组件索引,但我不知道如何实现 在外部组件的模板脚本中,
areas:
steps:
renderType: freemarker
availableComponents:
step:
id: obi-module:components/step
因此,我可以将N个相同类型的组件添加到步骤区域
问题:现在我想在步骤组件的模板脚本中显示组件索引,但我不知道如何实现
在外部组件的模板脚本中,我可以执行以下操作:
[#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]
这显示了所需的基于1的索引编号,但现在在Magnolia中,我无法再编辑组件,因为我没有获得步骤组件的典型绿色Magnolia编辑器栏
当我像这样放置模板脚本时:
[#assign stepList=cmsfn.children(content.steps) ]
...
<p>Number of steps: ${stepList?size!'#'}</p>
...
[@cms.area name="steps" /]
[#list components as component]
[@cms.component content=component contextAttributes={'index':component?counter} /]
[/#list]
[#分配步骤列表=cmsfn.children(content.steps)]
...
步骤数:${stepList?size!'#'}
...
[@cms.area name=“步骤”/]
我可以编辑组件,但我不知道如何获取(列表)索引
有人知道如何做到这一点吗?这是一个名为
\u index
的freemarker内置程序。。。你可以试试这个:
[#list components as component ]
Component Number: '${component_index + 1!}'
[@cms.component content=component /]
[/#list]
编辑:如果不起作用,请尝试
component?index
而不是component_index
。。。这取决于您正在使用的magnolia版本的magnolia随附的freemarker版本。我的问题-我在(外部)步骤组件的脚本中迭代步骤组件,但我必须在区域脚本中执行。因此,我将步骤组件定义更改为:
areas:
steps:
renderType: freemarker
templateScript: /my-module/components/steps-area.ftl
availableComponents:
step:
id: my-module:components/step
区域模板脚本(steps-area.ftl)如下所示:
[#assign stepList=cmsfn.children(content.steps) ]
...
<p>Number of steps: ${stepList?size!'#'}</p>
...
[@cms.area name="steps" /]
[#list components as component]
[@cms.component content=component contextAttributes={'index':component?counter} /]
[/#list]
我对木兰花特有的部分一无所知,但注意到一些无关的东西<代码>${list?size!'}永远不会打印
。如果您想在缺少list
时执行此操作,则需要${(list?size)!'#'}
。嘿,Bradley,在(外部)步骤组件的脚本中呈现索引,我通过contextAttributes将其传递给内部步骤组件的脚本。但是,当我在组件上迭代时,在内部步骤组件周围没有绿色的Magnolia编辑器栏来配置它们。当我使用@cms.area freemarker指令时,我只有这些绿色条。