Is there a way to get the page properties of absolute parent using Sightly? | Community
Skip to main content
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
BrianKasingli
BrianKasingliAccepted solution
New Participant
Nov 19, 2021 7:21 PM UTC

Yeah sure, you can use sling model to get the parent Page object, and then use the page API from sightly to obtain such values.

I am 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-consumption

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;
	}
}

 

 

 

5 replies

BrianKasingli
BrianKasingliAccepted solution
New Participant
Nov 19, 2021 7:21 PM UTC

Yeah sure, you can use sling model to get the parent Page object, and then use the page API from sightly to obtain such values.

I am 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-consumption

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;
	}
}

 

 

 

Translate
SHIBANI06Author
Level 4
Nov 22, 2021 9:02 AM UTC

Hi @briankasingli ,

 

I was looking for a way to get the absolute parent page properties only using sightly but looks like we will have to make use of sling model in order to achieve this.

Thank you.

Translate
Asutosh_Jena_
New Participant
Nov 19, 2021 10:24 AM UTC

Hi @shibani06 

 

It is not possible to get the value in HTL only, as the absolute parent page can be at "n"th level.

 

So you need to use Sling Model to retrive the dynamic value.

 

Returns the absolute parent page. If no page exists at that level, null is returned. Example (this path == /content/geometrixx/en/products)

 | level | returned                        |
 |     0 | /content                        |
 |     1 | /content/geometrixx             |
 |     2 | /content/geometrixx/en          |
 |     3 | /content/geometrixx/en/products |
 |     4 | null                            |

https://www.adobe.io/experience-manager/reference-materials/6-5/javadoc/com/day/cq/wcm/api/Page.html#getAbsoluteParent-int-

 

Hope this helps! 

Translate
SHIBANI06Author
Level 4
Nov 19, 2021 11:34 AM UTC

Thank you @asutosh_jena_ 

Translate
Bhuwan_B
New Participant
Nov 19, 2021 9:32 AM UTC

@shibani06 Please try similar to below Code snippet:

${currentPage.parent.properties['jcr:title']}

 Also, Please check below Community URL for similar ask:

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/how-to-get-absolutepath-or-absoluteparent-in-sightly/td-p/198050

Translate