Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

Checking an url if it contains specific text

Avatar

Avatar
Validate 1
Level 1
sai_santhosht50
Level 1

Likes

3 likes

Total Posts

22 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Boost 3
Boost 1
View profile

Avatar
Validate 1
Level 1
sai_santhosht50
Level 1

Likes

3 likes

Total Posts

22 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Boost 3
Boost 1
View profile
sai_santhosht50
Level 1

24-09-2020

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.

Package Manager sightly

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Boost 500
MVP
Vijayalakshmi_S
MVP

Likes

511 likes

Total Posts

647 posts

Correct Reply

213 solutions
Top badges earned
Boost 500
Give Back 50
Give Back 5
Ignite 10
Ignite 5
View profile

Avatar
Boost 500
MVP
Vijayalakshmi_S
MVP

Likes

511 likes

Total Posts

647 posts

Correct Reply

213 solutions
Top badges earned
Boost 500
Give Back 50
Give Back 5
Ignite 10
Ignite 5
View profile
Vijayalakshmi_S
MVP

24-09-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.

 

Answers (2)

Answers (2)

Avatar

Avatar
Give Back
Level 4
Jineet_Vora
Level 4

Likes

42 likes

Total Posts

45 posts

Correct Reply

22 solutions
Top badges earned
Give Back
Ignite 1
Boost 5
Boost 3
Boost 25
View profile

Avatar
Give Back
Level 4
Jineet_Vora
Level 4

Likes

42 likes

Total Posts

45 posts

Correct Reply

22 solutions
Top badges earned
Give Back
Ignite 1
Boost 5
Boost 3
Boost 25
View profile
Jineet_Vora
Level 4

24-09-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

Avatar

Avatar
Validate 1
MVP
raj_mandalapu
MVP

Likes

124 likes

Total Posts

203 posts

Correct Reply

72 solutions
Top badges earned
Validate 1
Contributor
Shape 1
Give Back 5
Give Back 3
View profile

Avatar
Validate 1
MVP
raj_mandalapu
MVP

Likes

124 likes

Total Posts

203 posts

Correct Reply

72 solutions
Top badges earned
Validate 1
Contributor
Shape 1
Give Back 5
Give Back 3
View profile
raj_mandalapu
MVP

24-09-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 

@inject

private Page currentPage;

3: check path contains /cd/

 

 

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