AEM 6.0悦目子节点 [英] AEM 6.0 Sightly Child Nodes

查看:298
本文介绍了AEM 6.0悦目子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我身边有使用悦目访问组件的子节点的问题。我在使用数据狡猾的资源,像这样一个基本的图像组件,它拉的模板。

I have a question around using Sightly to access child nodes of a component. I have a template which pulls in a basic image component using data-sly-resource, like so.

<div class="${wcmmode.edit ? 'image-edit image' : 'image'}" data-sly-resource="${ 'heroImage' @ resourceType='/libs/foundation/components/image', appendPath='image', selectors='fileReference' }"> </div>

我希望做的是改变基于该图像组件是否真正具有图像集的CSS类。对于这个我的计划是进入到图像组件节点并阅读其文件引用。沿行的东西

What I would like to do is change the css class based on whether or not that image component actually has an image set. For this my plan was to access to the image component node and read its file reference. Something along the line of

<h1>${ properties["heroImage"] }</h1>

不幸的是,这并不正常工作。我的问题是我怎么能访问heroImage资源的FileReference从我的模板,看到它的一个子节点。

Unfortunately this doesn't work. My question is how can I access the fileReference of the heroImage resource from my template, seeing as its a child node.

谢谢, 哈利

推荐答案

在AEM6,所以不可能直接从悦目模板访问的子节点和它们的性质无preparing数据使用-API中。

In AEM6, it isn't possible to access child nodes and their properties directly from the Sightly template without preparing the data in the Use-API.

这是一种方式,你如何prepare的数据,所以在你的CQ组件,你通常有类似以下两个文件。

This is one way how you can prepare that data, so in your CQ component, you'd typically have something like following two files.

<!-- template.html -->
<h1 data-sly-use.logic="Logic">
    ${logic.heroImage.fileReference}
</h1>

<!-- Logic.java -->
package apps.PATH.TO.YOUR.COMPONENT.FOLDER;

import com.adobe.cq.sightly.WCMUse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;

public class Logic extends WCMUse {
    private static final String CHILD_NODE = "heroImage";
    private ValueMap childProperties;

    @Override
    public void activate() throws Exception {
        Resource childResource = getResource().getChild(CHILD_NODE);
        childProperties = childResource.adaptTo(ValueMap.class);
    }

    public ValueMap getHeroImage() {
        return childProperties;
    }
}

您还可以将Logic.java文件转换成一个OSGi包,那么你会明显变化包名,并在模板中调用这个类,你接下来要提供完全合格的包名称:&LT; H1数据SLY-use.logic =com.PATH.TO.YOUR.PACKAGE.Logic&GT;

You can also move the Logic.java file into an OSGi bundle, then you'd obviously change the package name, and in the template to call that class, you'd then have to provide the fully qualified package name: <h1 data-sly-use.logic="com.PATH.TO.YOUR.PACKAGE.Logic">

希望帮助, 加布里埃尔

Hope that helps, Gabriel

这篇关于AEM 6.0悦目子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆