Expand my Community achievements bar.

SOLVED

Checking an url if it contains specific text

Avatar

Level 2

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.

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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.

 

View solution in original post

11 Replies

Avatar

Level 8

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 

@inject

private Page currentPage;

3: check path contains /cd/

 

 

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

 

 

 

Avatar

Correct answer by
Community Advisor

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.

 

Avatar

Community Advisor

In the activate method of WCMUsePojo, use the below snippet to get the current page path and hence contains check on it.

String actualPagePath = getCurrentPage().getPath();
if(actualPagePath.contains("retail")) {
LOG.info("It is a we-retail page !!");
}

Perform any manipulations or further logic here and expose things that are needed in HTL via getters from POJO.

Avatar

Community Advisor

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

Avatar

Level 2

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.

Avatar

Level 2

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

${'cd' in currentPage.path}

Hope that helps

Avatar

Administrator

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



Kautuk Sahni

Avatar

Level 4

Hi RajarshiRoy

 

${'cd' in currentPage.path}

above correct if char exist in String. How we check not in. Because 'not' or '!' is not working.

Avatar

Level 1

Hi @sanketd27011989 

 

You can do like this:

 

first store result of this expression in a variable using:  dat-sly-test.[variable name]

 

E.g:  <sly data-sly-test.variableName = "${'cd' in currentPage.path}"></sly>

 

The result of expression will be either - true or false.

Hence you can use this variable to check for both conditions like:

 

E.g: 

// just pass test variable to data-sly-test. it will check if value is true, then expression will evaluate else it will exit the expression.

<sly data-sly-test = "${ variableName }">

      // evaluate expression for true condition

</sly>

 

// Append NOT operator ( ! ) before test variable . this will test for false.

<sly data-sly-test = "${ !variableName }">

      // evaluate expression for false condition

</sly>