How to display page properties of 2 level up page | Community
Skip to main content
Level 2
August 2, 2022
Solved

How to display page properties of 2 level up page

  • August 2, 2022
  • 2 replies
  • 718 views

Hi All,

I have requirement to display parent page properties in the child page which is 2 level up

example:homepage->page1->page2

    here i need to display properties of homepage in the page2 page

 

anyone work on this requirement.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by SantoshSai

Hi @bangar20 

You can use Sling Model to get the parent Page object, and then use the page API from sightly to obtain such values.

Using constructor injection as the example below as this strategy can also reduce memory consumption of your application, https://sourcedcode.com/blog/aem/aem-sling-model-field-injection-vs-constructor-injection-memory-con...

Sightly:

<sly data-sly-use.mycomponent="com.mysite.models.components.MyComponent"></sly>
${mycomponent.properties['jcr:title']}

Sling Model:

import com.day.cq.wcm.api.Page;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.ScriptVariable;

import javax.inject.Inject;
import javax.inject.Named;

@Model(adaptables = Resource.class,
	defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class MyComponent {

	private Page absParentPage;

	@Inject
	public MyComponent(@ScriptVariable @Named("currentPage") final Page currentPage) {
		Page parentPage = currentPage.getAbsoluteParent(2);
		if (parentPage != null) {
			absParentPage = parentPage;
		}
	}

	public Page getAbsParentPage() {
		return absParentPage;
	}
}

Hope that helps!

Regards,

Santosh 

2 replies

SantoshSai
Community Advisor
SantoshSaiCommunity AdvisorAccepted solution
Community Advisor
August 2, 2022

Hi @bangar20 

You can use Sling Model to get the parent Page object, and then use the page API from sightly to obtain such values.

Using constructor injection as the example below as this strategy can also reduce memory consumption of your application, https://sourcedcode.com/blog/aem/aem-sling-model-field-injection-vs-constructor-injection-memory-con...

Sightly:

<sly data-sly-use.mycomponent="com.mysite.models.components.MyComponent"></sly>
${mycomponent.properties['jcr:title']}

Sling Model:

import com.day.cq.wcm.api.Page;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.ScriptVariable;

import javax.inject.Inject;
import javax.inject.Named;

@Model(adaptables = Resource.class,
	defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class MyComponent {

	private Page absParentPage;

	@Inject
	public MyComponent(@ScriptVariable @Named("currentPage") final Page currentPage) {
		Page parentPage = currentPage.getAbsoluteParent(2);
		if (parentPage != null) {
			absParentPage = parentPage;
		}
	}

	public Page getAbsParentPage() {
		return absParentPage;
	}
}

Hope that helps!

Regards,

Santosh 

Santosh Sai
Shiv_Prakash_Patel
Community Advisor
Community Advisor
August 3, 2022

Hi @bangar20 ,

You can achieve this requirement with the help of HTL Global Objects in Sightly.

Sightly Code :

2nd Level Page Path : ${currentPage.parent.parent.path} </br>
2nd Level Custom Page Properties : ${currentPage.parent.parent.properties['countryName']} </br>
2nd Level Page Title : ${currentPage.parent.parent.properties['jcr:title']} </br>
1st Level Page Title : ${currentPage.parent.properties['jcr:title']} </br>
0 Level or Current Page Title : ${currentPage.properties['jcr:title']} </br>
Or : ${pageProperties['jcr:title']} </br>
Or : ${currentPage.title}

 Component Sceenshot :

Hope this could help you !!!

Regards,

Shiv

Shiv Prakash