Using Page Properties Dynamically in Initial Page Template(AEM) | Community
Skip to main content
May 7, 2025
Solved

Using Page Properties Dynamically in Initial Page Template(AEM)

  • May 7, 2025
  • 3 replies
  • 694 views

Is there a way to use page property values in the initial template? For example, I’ve added a text component in initial/.content.xml—can I dynamically use the page title from the page properties as the text value in this component?

<text
jcr:created="{Date}2024-03-06T01:11:46.178-05:00"
jcr:createdBy="admin"
jcr:lastModified="{Date}2024-03-06T20:43:19.059-05:00"
jcr:lastModifiedBy="admin"
jcr:primaryType="nt:unstructured"
sling:resourceType="ABC/components/content/text"
text="Page Title"/>

 

Best answer by SantoshSai

Hi @abilashra,

I think, you cannot dynamically set values like the page title in the initial/.content.xml because that XML is static and processed at template creation time, before the page is created - so dynamic values like jcr:title from page properties aren’t available yet.

May be you could try below 2 options:

1. Use HTL to reference page title at runtime

Instead of hardcoding a value in .content.xml, let the HTL (Sightly) template read the page title dynamically:

<h1>${pageProperties['jcr:title']}</h1>
  • No need to modify your model or text component logic.
  • Only works if your component supports rendering via HTL.

2. Use a custom page creation workflow or listener

If you truly want to populate the component’s property (e.g., text) with the page title at creation time, you’ll need to:

  • Write a custom page creation event listener or workflow step.

  • Programmatically read the page’s jcr:title and write it into the text property of the embedded component node.

Example logic (in a servlet or listener):

Node textNode = page.getContentResource("root/text").adaptTo(Node.class);
String title = page.getProperties().get("jcr:title", "");
textNode.setProperty("text", title);

I don't think you can reference ${pageProperties['jcr:title']} directly in .content.xml like:

text="${pageProperties['jcr:title']}"  <!-- This won't work -->

because the XML is not evaluated dynamically.

Hope that helps!

3 replies

giuseppebaglio
Level 10
May 7, 2025

Yes, it can be done by updating the logic of the text component.

You can inject into your Sling Model the object currentPage:

String pageTitle; @SlingObject private Page currentPage ... @PostConstruct protected void init() { this.pageTitle = (String) currentPage.getProperties().get("jcr:title"); } ... public String getPageTitle() { return this.pageTitle; } ... <div data-sly-use.model="com.project.your.sling.Model" class="text-component"> <h1>${model.pageTtile}</h1> </div>

or with HTL you can directly leverage the global object pageProperties like so:

<h1>${pageProperties['jcr:title']}</h1>

 

 

 
AbilashRaAuthor
May 7, 2025

@giuseppebaglio , 
Thank you so much for the reply. I don’t want to edit the text component since it’s used on many other pages—I just want to update the template by setting a default value for the text component.

SantoshSai
Community Advisor
SantoshSaiCommunity AdvisorAccepted solution
Community Advisor
May 7, 2025

Hi @abilashra,

I think, you cannot dynamically set values like the page title in the initial/.content.xml because that XML is static and processed at template creation time, before the page is created - so dynamic values like jcr:title from page properties aren’t available yet.

May be you could try below 2 options:

1. Use HTL to reference page title at runtime

Instead of hardcoding a value in .content.xml, let the HTL (Sightly) template read the page title dynamically:

<h1>${pageProperties['jcr:title']}</h1>
  • No need to modify your model or text component logic.
  • Only works if your component supports rendering via HTL.

2. Use a custom page creation workflow or listener

If you truly want to populate the component’s property (e.g., text) with the page title at creation time, you’ll need to:

  • Write a custom page creation event listener or workflow step.

  • Programmatically read the page’s jcr:title and write it into the text property of the embedded component node.

Example logic (in a servlet or listener):

Node textNode = page.getContentResource("root/text").adaptTo(Node.class);
String title = page.getProperties().get("jcr:title", "");
textNode.setProperty("text", title);

I don't think you can reference ${pageProperties['jcr:title']} directly in .content.xml like:

text="${pageProperties['jcr:title']}"  <!-- This won't work -->

because the XML is not evaluated dynamically.

Hope that helps!

Santosh Sai
AmitVishwakarma
Community Advisor
Community Advisor
May 8, 2025

Hi @abilashra ,

You cannot use ${pageProperties['jcr:title']} or any dynamic reference in .content.xml:

text="${pageProperties['jcr:title']}" <!-- This does NOT work -->

Because .content.xml is processed only once when the page is created from the template—and it is treated as static content. At that time, the actual page properties like jcr:title are not yet available for interpolation.

Solution:

To auto-populate the text property of the component based on the page title during page creation, you must use an Event Listener or Page Creation Workflow. Here’s how.

Option 1: Use a Page Creation Listener (Recommended for AEM 6.5)

Try below steps:

Create a listener (OSGi component) that listens for page creation events.

Read the jcr:title of the page.

Write that value into the text property of the component.

Sample Java Code:

@Component(immediate = true, service = EventHandler.class, property = { EventConstants.EVENT_TOPIC + "=" + PageEvent.EVENT_TOPIC }) public class PageCreationListener implements EventHandler { @Reference private ResourceResolverFactory resolverFactory; @Override public void handleEvent(Event event) { PageEvent pageEvent = PageEvent.fromEvent(event); for (String path : pageEvent.getAddedPaths()) { if (!path.contains("/content")) { continue; } try (ResourceResolver resolver = resolverFactory.getServiceResourceResolver( Collections.singletonMap(ResourceResolverFactory.SUBSERVICE, "datawriter"))) { PageManager pageManager = resolver.adaptTo(PageManager.class); Page page = pageManager.getPage(path); if (page == null) return; String pageTitle = page.getProperties().get("jcr:title", ""); Resource textComponent = resolver.getResource(page.getPath() + "/jcr:content/root/text"); if (textComponent != null) { ModifiableValueMap props = textComponent.adaptTo(ModifiableValueMap.class); props.put("text", pageTitle); resolver.commit(); } } catch (Exception e) { // log error } } } }

This runs once at page creation, so it doesn’t impact runtime or page performance.

Option 2: Workflow Step (Alternate Approach)

If you use a custom page creation workflow, add a process step that does the same logic as above—reading the title and updating the component.

Note on Permissions:

Ensure the service user (e.g., datawriter) has write access to the component node. Configure it via /system/console/jcrresolver and /home/users/system.

Regards,
Amit

AbilashRaAuthor
May 8, 2025

Thank You @santoshsai @amitvishwakarma @giuseppebaglio , 
All the approaches work fine. I got the idea from all of them, retrieved the template type, and then, only for that template type, set the text property as the page title in the Sling model. I can't mark all the answers as correct above, but thanks to everyone!