Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events
SOLVED

How can I get the HTML document for the current page

Avatar

Level 4

then, after updating the HTML, have that page rendered, (essentially, updating the page dynamically and displaying the updated HTML document)? This would be done by a component, so that whatever page it was included on it would 1) Get the page's HTML, 2) Analyze the HTML, and potentially update it,  3) then, display the updated HTML in place of the original HTML, (i.e. with any new elements, javascript, CSS etc.? Ideally, I'd like to update the page's representation in the JCR, so that the dynamic logic won't have to be run everytime the page is loaded, however, if there's no way to do this, I'll just have to incur the cost of running it everytime the page is loaded. Please detail the steps required to do this.

1 Accepted Solution

Avatar

Correct answer by
Level 10

Its not best practice to dynamically modify AEM generated HTML code that is based on components, etc. A better way to dynamically inject scripts and content is to use Dynamic Tag Manager. See:

http://www.adobe.com/ca/solutions/digital-marketing/dynamic-tag-management.html

View solution in original post

9 Replies

Avatar

Employee Advisor

I am not sure, what you want to achieve with this. What are your requirements?

kind regards,
Jörg

Avatar

Level 4

The requirements are to be able to locate HTML elements in the current page document, inject HTML snippets, (and possibly code snippets), immediately before or after the elements, (e.g. inject HTML immediately before the closing </body> element, or to right a script snippet in the <head>), and then have the updated HTML rendered in place of the page's original HTML. Of course, it'd be best to update the page in the JCR, so that the changes are persistent, but if that's not possible, it'll have to be done dynamically each time the page is loaded.

Avatar

Employee Advisor

Hi,

I do understand, that this is your approach to handle the requirement, but still I don't understand the reason, why you want to do this.

If you have the HTML of the page under your full control, why don't you enhance your templates and components in a way, that you don't need to parse the HTML again.

kind regards,
Jörg

Avatar

Level 4

Jorg, the primary reason is to be able to interject HTML and script DYNAMICALLY, (or, delete, or update script, add simple HTML etc.), based on some business logic. This will eliminate the need to have developers touch code to update simple analytics scripts etc., and allow authors to achieve this by dragging and dropping this component on a page. If you know how to achieve this, please help. These types of features are MANDATED by management, even though it's not the common approach.

Avatar

Correct answer by
Level 10

Its not best practice to dynamically modify AEM generated HTML code that is based on components, etc. A better way to dynamically inject scripts and content is to use Dynamic Tag Manager. See:

http://www.adobe.com/ca/solutions/digital-marketing/dynamic-tag-management.html

Avatar

Level 8

I don't know that i truly understand what you're trying to accomplish, but the business requirement sounds pretty out there and impossible.  I think maybe it'd be worth going back to management and teaching them the best practices of a CMS and websites, rather than trying to tailor a CMS to do something it's not supposed to do.

Avatar

Level 4

smacdonald2008 wrote...

Its not best practice to dynamically modify AEM generated HTML code that is based on components, etc. A better way to dynamically inject scripts and content is to use Dynamic Tag Manager. See:

http://www.adobe.com/ca/solutions/digital-marketing/dynamic-tag-management.html

 

 

Thanks, and I've certainly considered using DTM, (which satisfies several of the use-cases), but does DTM allow you to specify where the scripts and/or HTML is injected into the page, (i.e. before or after a particular HTML element)??

Avatar

Administrator

Hi

As stated by many expert that this is not a good practice. 

But, still to do so, please find below the link that can act as some help to you.

Link:- http://www.nateyolles.com/blog/2015/10/get-rendered-html-for-an-aem-resource-or-component?scid=socia...

//

It's easy to retrieve a rendered component by making a GET request. Apache Sling's SlingMainServlet and DefaultGetServletwill process GET requests, take the path's extension into account, and return the rendered resource. The most obvious example of triggering this process is simply typing the component's path into the browser's address bar or making an AJAX call. You can retrieve the HTML markup (or JSON, XML, txt, PDF, etc...) for a component as well as a page if you provide the correct path. After all, they're both just Sling resources. 
 
Trying to get the rendered HTML of a component on the server side is still easy, but requires a little more work. Without knowing the inner workings of Sling, you might use Java's java.net.HttpUrlConnection class to construct and make an HTTP request from within your application to your application.
 
The better way is to use the SlingRequestProcessor service as the entry point into Sling's process. The processRequest method of the SlingRequestProcessor takes an HttpServletRequest, HttpServletResponse, and Resource Resolver as parameters. The only trick is that you need to provide a request that enables you to set the request path and a response that enables you to get the OutputStream as a String.

 

Link:- https://helpx.adobe.com/experience-manager/using/custom-sling-servlets.html  (Example to show you how to do get/post call and responding to it).

 

I hope this could help you.

Thanks and Regards

Kautuk Sahni 



Kautuk Sahni

Avatar

Level 4

Kautuk, thanks a TON for the suggestions. I will try this to see if I can get it to work, and if it works, all I'll have to do is persist the modified page back to the JCR. I hate having to hack things in place, but until I gain the confidence of management, I can't continue explaining/arguing over whether this is the correct/best way to do it, and I just have to go ahead and do what I'm told/asked to do. If anyone else has alternate ideas, I'd love to hear them too.