Expand my Community achievements bar.

SOLVED

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

Avatar

Level 1

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 

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

View solution in original post

3 Replies

Avatar

Community Advisor

@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.

Avatar

Correct answer by
Community Advisor

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

Avatar

Level 7

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!