【问题标题】:How to conditionally add a class to a Magnolia component template based on the presence of another Component?如何根据另一个组件的存在有条件地将类添加到 Magnolia 组件模板?
【发布时间】:2019-10-01 19:15:50
【问题描述】:

仅当内容编辑器选择将字幕组件添加到页面时,我想将一个类应用于我在 Magnolia 中的标题组件。我在下面尝试做的示例:

[#assign subtitle = "somehow get a reference to the subtitle component"]
[#assign subtitleExists = subtitle?has_content]
<div class="title ${subtitleExists?then('removePadding','')}">
  ${model.render('titleCopy')!}
</div>

这可能吗?如果需要,我很高兴添加更多信息以帮助澄清我在这里尝试做的事情。谢谢!

【问题讨论】:

    标签: freemarker magnolia


    【解决方案1】:

    组件是原子的,因此它们通常看不到彼此,也不会相互影响。那将是对组件或父组件或页面本身进行分组的区域的工作。

    也就是说,如果页面和模板的结构不允许您使用任何其他解决方案,您可以通过字幕组件中的请求属性设置一些标志,然后检查组件中是否存在所述标志。 一边是ctx.setAttribute("subtitleExists", true, 1),另一边是ctx.getAttribute("subtitleExists")。 1 代表属性的本地(请求)范围。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多