In this code snippet, I am going to explain how to get page object by passing page path, in the activate method get the path and assign it to the local variable, you need to pass this local variable to the PageManager.getContainingPage method to get the page object

page = pageManager.getContainingPage(pagePath);

Complete code :

package com.aem.toolkit.core.models;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.ResourceResolver;
import com.adobe.cq.sightly.WCMUsePojo;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
public class Example1 extends WCMUsePojo {
	private String pagePath;
	@Override
	public void activate() throws Exception {
		pagePath = get("path", String.class);
	}
	public String getPageTitle() {
		return getPage().getTitle();
	}
	public Page getPage() {
		Page page = null;
		if (null == getRequest()) {
			return page;
		}
		ResourceResolver resolver = getRequest().getResourceResolver();
		if (StringUtils.startsWith(pagePath, "/") && null != resolver) {
			PageManager pageManager = resolver.adaptTo(PageManager.class);
			page = pageManager.getContainingPage(pagePath);
			pageManager = null;
		}
		return page;
	}
}

Use below html code to print page title

<div data-sly-use.info="${'com.aem.toolkit.core.models.Example1' @ path='/content/toolkit/home'}">
    <p>${info.pageTitle}</p>
</div>

The output should display your page title

By coderss

Leave a Reply

Your email address will not be published. Required fields are marked *