The decoration tag is added by a filter (namely the WCMComponentFilter) before the component is actually rendered, so it isn't possible to change it inside the component code. In order to use some logic to dynamically set a CSS class on this decorator, you need to create a custom filter, that will run before the WCMComponentFilter and set appropriate properties to the IncludeOptions attribute.
Following filter adds my-class to all carousel components under /content/geometrixx/en.
@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);
}