Expand my Community achievements bar.

SOLVED

How to display page properties of 2 level up page

Avatar

Level 2

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.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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 

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

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 

Avatar

Community Advisor

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 :

Test Page.jpg

Hope this could help you !!!

Regards,

Shiv

Shiv Prakash