What is the best way to get current page title in a sling model in aem | Community
Skip to main content
New Member
May 9, 2024
Solved

What is the best way to get current page title in a sling model in aem

  • May 9, 2024
  • 3 replies
  • 4261 views

Can anyone please tell me the which way is most optimized way  to get the current page title in a sling model , should i use @scriptVariable and get currentPage or can is use @Named("jcr:title") or any other best approach 

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 HeenaMadan

Utilize the Page object available in the Sling model.

 

 

package com.test.core.models; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.injectorspecific.Self; import org.apache.sling.models.annotations.injectorspecific.SlingObject; import com.day.cq.wcm.api.Page; @Model(adaptables = SlingHttpServletRequest.class) public class PageTitleModel { @Self private SlingHttpServletRequest request; @SlingObject private Page currentPage; public String getPageTitle() { if (currentPage != null) { return currentPage.getTitle(); } else { return "No title available"; } } } or get by PageManager PageManager pageManager = resourceResolver.adaptTo(PageManager.class); Page currentPage = pageManager.getContainingPage(resource); pageTitle = currentPage.getTitle();

Thanks

3 replies

Kamal_Kishor
Community Advisor
Community Advisor
May 10, 2024

@fantasticfour555 : 
@ValueMapValue
@Named("jcr:title") - This will give you the value of 'jcr:title' property of the component which is being mapped to your sling model. This would never give you page title.

You will always have to get it from currentPage object by calling the appropriate method.

OR if you are OK to get it directly in your sightly you can do it like this as currentPage object is already available in sightly.

${currentPage.title}

thanks.

HeenaMadan
Community Advisor and Adobe Champion
HeenaMadanCommunity Advisor and Adobe ChampionAccepted solution
Community Advisor and Adobe Champion
May 10, 2024

Utilize the Page object available in the Sling model.

 

 

package com.test.core.models; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.injectorspecific.Self; import org.apache.sling.models.annotations.injectorspecific.SlingObject; import com.day.cq.wcm.api.Page; @Model(adaptables = SlingHttpServletRequest.class) public class PageTitleModel { @Self private SlingHttpServletRequest request; @SlingObject private Page currentPage; public String getPageTitle() { if (currentPage != null) { return currentPage.getTitle(); } else { return "No title available"; } } } or get by PageManager PageManager pageManager = resourceResolver.adaptTo(PageManager.class); Page currentPage = pageManager.getContainingPage(resource); pageTitle = currentPage.getTitle();

Thanks

tushaar_srivastava
Level 6
May 10, 2024

Hi @fantasticfour555 

 

You can use the @ScriptVariable annotation to get the current page in a Sling Model. Once you have the current page, you can use the Page API to get the title.

 

The @Named("jcr:title") annotation is used to inject a property from the current resource, not the current page.

 

If the current resource is a page, @Named("jcr:title") will inject the jcr:title property of the page.

 

However, this is not the same as the title of the page.

The title of the page is a property of the jcr:content node of the page, not the page node itself.

 

Hence the optimized way to get the currentPage Title in Sling Model and use @ScriptVariable to get current Page and Page API to get title

 

Hope I am able to clarify your question!