首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AEM/CQ:装饰标签上的条件CSS类

AEM/CQ:装饰标签上的条件CSS类
EN

Stack Overflow用户
提问于 2014-09-29 22:25:47
回答 2查看 4.2K关注 0票数 4

如何在AEM6 Sightly组件的包装装饰标签上动态设置CSS类?

我不能在组件上设置这个CSS类,因为它依赖于组件的实例,我也不能在资源上设置它,因为资源可以呈现在多个页面上,而且CSS类根据它在哪个页面上而有所不同。

我已经在JavaScript的Use-API中尝试了以下3种技术,但都没有成功:

代码语言:javascript
复制
componentContext.getCssClassNames().add('test-class-1');
component.getHtmlTagAttributes().set('class', 'test-class-2');//throws an exception
currentNode.setProperty('cq:cssClass', 'test-class-3');
EN

回答 2

Stack Overflow用户

发布于 2014-09-30 18:14:08

装饰标记是在组件实际呈现之前由过滤器(即WCMComponentFilter)添加的,因此不可能在组件代码中更改它。为了使用某些逻辑在此装饰器上动态设置CSS类,您需要创建一个自定义过滤器,该过滤器将在WCMComponentFilter之前运行,并为IncludeOptions属性设置适当的属性。

下面的过滤器将my-class添加到/content/geometrixx/en下的所有轮播组件。

代码语言:javascript
复制
@SlingFilter(scope = SlingFilterScope.COMPONENT, order = -300)
public class DecoratorFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        boolean addClass = false;
        if (request instanceof SlingHttpServletRequest) {
            final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;
            final Resource resource = slingRequest.getResource();
            final String resourceType = resource.getResourceType();
            final String resourcePath = resource.getPath();

            // put any custom logic here, eg.:
            addClass = "foundation/components/carousel".equals(resourceType)
                    && resourcePath.startsWith("/content/geometrixx/en");
        }
        if (addClass) {
            final IncludeOptions options = IncludeOptions.getOptions(request, true);
            options.getCssClassNames().add("my-class");
        }
        chain.doFilter(request, response);
    }
票数 9
EN

Stack Overflow用户

发布于 2018-01-29 22:32:33

正确的方法是使用cq:htmlTag。在组件下创建名为cq:htmlTag的非结构化节点

添加一个属性" class“,类型为"String”,值为"myclass“,即您想要添加到组件的包装器/装饰器div中的类。

参考链接:

1) Wrapper Div Manipulation

2) Best link to understand this

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26102239

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档