How to propagate template code changes to sites using that template | Community
Skip to main content
rkody
November 7, 2024
Solved

How to propagate template code changes to sites using that template

  • November 7, 2024
  • 5 replies
  • 966 views

Is there an easy way to propagate the changes to a template in code to the pages that use that template? Without making brand new pages.

 

I have added components and moved them around.

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 SureshDhulipudi

You can use a Groovy script to update existing pages that use the modified template.

5 replies

SureshDhulipudi
Community Advisor
SureshDhulipudiCommunity AdvisorAccepted solution
Community Advisor
November 7, 2024

You can use a Groovy script to update existing pages that use the modified template.

anupampat
Community Advisor
Community Advisor
November 7, 2024

Curl Command, Groovy script & Managed Controlled Process.

Tethich
Community Advisor
Community Advisor
November 8, 2024

Hi @rkody 

No easy way. But rewarding way, yes.

 

You can also develop you own TouchUI tool, an integrate it into your project. For the beginning I imagine a simple page with a field for a path to update and a field for a template to lookup for. Of course you can refine it and make smarter by the time, but for now you might start with that.

 

Few considerations:

  • If you have an AMS team they will thank you (if you want to roll-out the template changes progressively by env or brand or whatever)
  • Plus, you could reuse it in other future projects (with few specific adjustments for each project probably)
  • Is an alternative if your team does not have Groovy skills.
donquixote_dofl
November 12, 2024

This approach using groovy might work

def resolver = getResourceResolver() try { String templatePath = "/content/my-site/templates/my-template" String componentPath = "/apps/my-site/components/my-component" Resource templateResource = resolver.getResource(templatePath) if (templateResource != null) { Node templateNode = templateResource.adaptTo(Node.class) Node componentNode = templateNode.addNode("myNewComponent", "cq:Component") componentNode.setProperty("sling:resourceType", componentPath) componentNode.setProperty("cq:template", templatePath) componentNode.setProperty("myCustomProperty", "value") // Save the changes templateNode.getSession().save() println "Component added successfully!" } else { println "Template not found!" } } catch (RepositoryException e) { println "An error occurred: ${e.message}" } finally { if (resolver != null) { resolver.close() } }
Manu_Mathew_
Community Advisor
Community Advisor
November 23, 2024

@rkody Are you using an editable or static template here?

If you modify the structure or policies of an editable template, the changes are automatically propagated to all pages that inherit from that template.

You could also write a script to update the affected pages programmatically like Groovy.

If the static template references a server-side script, any changes you make in the script will automatically be reflected on all pages that use the template.