Checking an url if it contains specific text | Community
Skip to main content
sai_santhosht50
Level 2
September 24, 2020
Solved

Checking an url if it contains specific text

  • September 24, 2020
  • 4 replies
  • 12892 views

There is a requirement in our project where i have to check if the current page path contains a specific text or not. How to write sightly code for this.

 

for ex:- <sly data-sly-test.pagepath=${currentPage.path}/> 

this will returns path as "/content/project/ab/cd/home/" now i want to check if it has " /cd/ " text in it.
Thanks in advance.

Best answer by Vijayalakshmi_S

Hi @sai_santhosht50,

HTL is for direct mark up rendering only. For any manipulation or conditional checks like this has to be done either in Use JS(make use of currentPage or request implied objects and "includes" javascript function for contains check) or Sling Model/WCMUsePojo.

 

4 replies

raj_mandalapu
Level 7
September 24, 2020

The sightly does not support this, you need to write java code, refer below link for writing the sling model

http://www.aemcq5tutorials.com/tutorials/adobe-aem-cq5-tutorials/sling-model-sightly-aem/

1. your class adaptable from SlingHttpServletRequest

2 add the following inject 

@586265

private Page currentPage;

3: check path contains /cd/

 

 

currentPage.path.contains("/cd/")

 

 

 

Vijayalakshmi_S
Vijayalakshmi_SAccepted solution
Level 10
September 24, 2020

Hi @sai_santhosht50,

HTL is for direct mark up rendering only. For any manipulation or conditional checks like this has to be done either in Use JS(make use of currentPage or request implied objects and "includes" javascript function for contains check) or Sling Model/WCMUsePojo.

 

sai_santhosht50
Level 2
September 24, 2020
@vijayalakshmi_s Can you please let me know how to use WCMUsePojo
Jineet_Vora
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
September 24, 2020

Hello @sai_santhosht50,

 

Instead of HTL (Sightly), you can use plain JavaScript or the JavaScript Use API to check if your string contains a specific keyword.

 

code.html <div data-sly-use.info="${'checkString.js' @ pagePath=currentPage.path}"> <h1>String contains /cd/: ${info.result}</h1> </div>

 

 

 

checkString.js "use strict"; use(function () { var result; var pagePath = this.pagePath; if(pagePath.includes('/cd/')) result=true; else result=false; return result; });

 

 

Hope this helps!

Jineet

sai_santhosht50
Level 2
September 24, 2020

Hi @Jinnet_Vora ,

Thanks for your reply.

The above mentioned concept i have already tried but the thing the js will apply after the css rendered on the page which shows some lag to the end user.

It will be help ful if you let me know java script use API with an example. 🙂

Level 2
October 5, 2021

Hi @sai_santhosht50 you can use a 'in' operator instead.. you can do it like

${'cd' in currentPage.path}

Hope that helps

kautuk_sahni
Community Manager
Community Manager
October 6, 2021

@rajarshiroy Good to have AEM SME like you in the community. Looking forward to more contribution from you. 

Kautuk Sahni