How to Use filter to parse and modify html to replace strings | Community
Skip to main content
Level 2
April 20, 2022
Solved

How to Use filter to parse and modify html to replace strings

  • April 20, 2022
  • 3 replies
  • 2124 views

Hi,

 

I am new to filters and would like to know how to modify the html on a page using them. My use case is such that I'm trying to dynamically look for certain strings in the html when rendering a page and replace them with another string (this is a very simplified version of what I'm trying to achieve but is a good summary).

 

I think filters would be a good candidate to achieve this however it is difficult to find resources on this. Any help is appreciated and I'd be happy to provide more details if necessary!

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 arunpatidar

Sling filter

Sling supports filtering the request processing by applying filter chains to the requests before actually dispatching to the servlet or script for processing. Filters to be used in such filter processing are plain OSGi services of type javax.servlet.Filter which of course means that the services implement this interface.

So ib this case, if you want to intercept the request you can use filter but for your use case it is not the best option

 

Sling Rewrite/Transformer

The Apache Sling Rewriter is a module for rewriting the output generated by a usual Sling rendering process. Some possible use cases include rewriting or checking all links in an HTML page, manipulating the HTML page, or using the generated output as the base for further transformation. An example of further transformation is to use XSLT to transform rendered XML to some output format like HTML or XSL:FO for generating PDF.

https://sling.apache.org/documentation/bundles/output-rewriting-pipelines-org-apache-sling-rewriter.html

 

 

 

HTML Parser can be used with either above of services.

 

3 replies

Ravi_Pampana
Community Advisor
Community Advisor
April 21, 2022

Hi,

 

With your use case, I think you can achieve with html parser instead of filter

 

Check the below link

https://aem4beginner.blogspot.com/creating-html-parser-service-for-adobe

rhamblin1Author
Level 2
April 21, 2022

This link is giving 404 not found. Do you have an alternate resource?

rhamblin1Author
Level 2
April 21, 2022

Nevermind, it redirects!

arunpatidar
Community Advisor
Community Advisor
April 21, 2022

In this case, you can do it using sling rewriter e.g.

https://wcm.io/handler/url/rewriter.html

 

Arun Patidar
rhamblin1Author
Level 2
April 21, 2022

@arunpatidar @ravi_pampana what would you say the difference would be between using a Filter vs HTML Parser vs Service vs Rewriter vs Transformer? It's pretty confusing the difference and I would like to understand the difference before making a decision!

arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
April 21, 2022

Sling filter

Sling supports filtering the request processing by applying filter chains to the requests before actually dispatching to the servlet or script for processing. Filters to be used in such filter processing are plain OSGi services of type javax.servlet.Filter which of course means that the services implement this interface.

So ib this case, if you want to intercept the request you can use filter but for your use case it is not the best option

 

Sling Rewrite/Transformer

The Apache Sling Rewriter is a module for rewriting the output generated by a usual Sling rendering process. Some possible use cases include rewriting or checking all links in an HTML page, manipulating the HTML page, or using the generated output as the base for further transformation. An example of further transformation is to use XSLT to transform rendered XML to some output format like HTML or XSL:FO for generating PDF.

https://sling.apache.org/documentation/bundles/output-rewriting-pipelines-org-apache-sling-rewriter.html

 

 

 

HTML Parser can be used with either above of services.

 

Arun Patidar