Render Conditions
via JSP
Path: /apps/myproject/renderconditions/myrendercondition/myrendercondition.jsp
<%@include file="/libs/granite/ui/global.jsp" %>
<%@page session="false"
import="org.apache.sling.api.resource.ResourceResolver,
com.adobe.granite.ui.components.rendercondition.RenderCondition,
com.something.myproject.core.servlets.MyRenderConditionServlet"
%>
<%
final ResourceResolver resolver = slingRequest.getResourceResolver();
request.setAttribute(RenderCondition.class.getName(), new MyRenderConditionServlet(resolver));
%>
Java - Servlet implements RenderCondition
Path: core/src/main/java/com/something/myproject/core/servlets/MyRenderConditionServlet.java
package com.something.myproject.core.servlets;
import com.adobe.granite.ui.components.rendercondition.RenderCondition;
import org.apache.jackrabbit.api.security.user.Group;
import org.apache.jackrabbit.api.security.user.User;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.ServletException;
import java.io.IOException;
import java.util.Iterator;
public class MyRenderConditionServlet implements RenderCondition {
private static final Logger LOG = LoggerFactory.getLogger(MyRenderConditionServlet.class);
private final ResourceResolver resolver;
public MyRenderConditionServlet(ResourceResolver resolver) {
this.resolver = resolver;
}
@Override
public boolean check() throws ServletException, IOException {
// do any checks here and return true, if render condition should match
return true;
}
}
Usage
Inside any item that can handle a granite:rendercondition
.
<granite:rendercondition
jcr:primaryType="nt:unstructured"
sling:resourceType="myproject/renderconditions/myrendercondition"/>
Java - as SafeMethodServlet
package com.something.myproject.core.servlets;
import com.adobe.granite.ui.components.Config;
import com.adobe.granite.ui.components.rendercondition.RenderCondition;
import com.adobe.granite.ui.components.rendercondition.SimpleRenderCondition;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.osgi.service.component.annotations.Component;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
// https://github.com/adobe/aem-core-wcm-components/blob/main/bundles/core/src/main/java/com/adobe/cq/wcm/core/components/internal/servlets/contentfragment/ElementNamesRenderCondition.java
@Component(
service = {Servlet.class},
property = {
"sling.servlet.resourceTypes=" + PathRenderCondition.RESOURCE_TYPE,
"sling.servlet.methods=GET"
}
)
public class SomeRenderCondition extends SlingSafeMethodsServlet {
// does not exist as a file -> only referenced by rendercondition block in xml
static final String RESOURCE_TYPE = "com.something.myproject.core.servlets/somerendercondition";
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws ServletException {
boolean shouldShow = false;
final Resource resource = request.getResource();
final Config config = new Config(resource);
final Long depth = config.get("someValue", Long.class);
// use configured values with any other value here to determine, if the component should render
shouldShow = someValue == 4;
request.setAttribute(RenderCondition.class.getName(), new SimpleRenderCondition(shouldShow));
}
}
Usage
Inside any item that can handle a granite:rendercondition
.
<granite:rendercondition
jcr:primaryType="nt:unstructured"
sling:resourceType="com.something.myproject.core.servlets/pathrendercondition"
someValue="{Long}7"/>