Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

Show/Hide Page Properties Based on Template in AEM | AEM Community Blog Seeding

kautuk_sahni
Community Manager
Community Manager

BlogImage.jpg

Show/Hide Page Properties Based on Template in AEM by Bimmisoi

Abstract

In AEM , editable templates usually share the same page component, which means the same page properties dialog.
Below are the steps to show/hide page properties based on the template.

1. Create a jsp under apps “/apps/project-name/granite/rendercondition/template/template.jsp” with below code.

<%@include file="/libs/foundation/global.jsp"%>
<%@page session="false"
import="com.adobe.granite.ui.components.Config,
com.adobe.granite.ui.components.rendercondition.RenderCondition,
com.adobe.granite.ui.components.rendercondition.SimpleRenderCondition,
com.core.utils.TemplateRenderConditionUtil,
org.apache.sling.api.resource.Resource,
org.apache.sling.api.resource.ValueMap,
com.adobe.granite.ui.components.ComponentHelper,
com.adobe.granite.xss.XSSAPI,
com.day.cq.i18n.I18n"%>

<%
final ComponentHelper cmp = new ComponentHelper(pageContext);
Config cfg = cmp.getConfig();
String path = cfg.get("templatePath", "");
boolean vote = TemplateRenderConditionUtil.isTemplate(slingRequest, request, path);
request.setAttribute(RenderCondition.class.getName(), new SimpleRenderCondition(vote));
%>

2. Now create a java file named TemplateRenderConditionUtil with below code as you can see we have used this java class in the jsp code.

import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.Template;
import java.util.Optional;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
public class TemplateRenderConditionUtil {
private static final String PAGE_PROPERTIES = "wcm/core/content/sites/properties";
public static boolean isTemplate(
SlingHttpServletRequest slingHttpServletRequest,
HttpServletRequest httpServletRequest,
String templatePath) {
// error if any of the passed params is null.
if (slingHttpServletRequest == null || httpServletRequest == null
|| StringUtils.isBlank(templatePath)) {
throw new IllegalArgumentException("One of the passed parameters is null.");
}
// the dialog is a page properties dialog
if (StringUtils.contains(httpServletRequest.getPathInfo(), PAGE_PROPERTIES)) {
// get the actual page path
String pagePath = httpServletRequest.getParameter("item");
// get page template path and check it
return Optional.ofNullable(slingHttpServletRequest.getResourceResolver())
.map(resourceResolver -> resourceResolver.getResource(pagePath))
.map(pageResource -> pageResource.adaptTo(Page.class))
.map(Page::getTemplate)
.map(Template::getPath)
.map(path -> StringUtils.contains(path, templatePath))
.orElse(false);
}
return false;
}
}

Read Full Blog

Show/Hide Page Properties Based on Template in AEM

Q&A

Please use this thread to ask the related questions.

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Reply
arvind
Level 4
Level 4

Hi @kautuk_sahni ,

 

Thank you for this post.

 

My Question:

 

Is there any possibilty to provide mutiple template paths for “templatePath”. I want to achieve same feature for more than 1 template. Thank you in advance.