Question to HTL or Sightly experts - What is wrong with the HTL expression "${'{0}' in title ? title @ format=currentPage.parent.title : title}"?

Avatar

Avatar
Ignite 1
Level 1
Sravan_Kumar_Si
Level 1

Like

1 like

Total Posts

13 posts

Correct reply

1 solution
Top badges earned
Ignite 1
Validate 1
Boost 1
Affirm 1
View profile

Avatar
Ignite 1
Level 1
Sravan_Kumar_Si
Level 1

Like

1 like

Total Posts

13 posts

Correct reply

1 solution
Top badges earned
Ignite 1
Validate 1
Boost 1
Affirm 1
View profile
Sravan_Kumar_Si
Level 1

04-12-2020

model.title contains one of the two values - "Back to {0}" or "Back"
<sly data-sly-test.title="${model.title}"/> 

<a>${'{0}' in title ? title @ format=currentPage.parent.title : title}</a>

 

Neither

${'{0}' in title ? title @ format=currentPage.parent.title : title}

nor

${'{0}' in title ? (title @ format=currentPage.parent.title) : title}

gives the expected result. What is the correct way?

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Establish
MVP
Anudeep_Garnepudi
MVP

Likes

275 likes

Total Posts

320 posts

Correct reply

87 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile

Avatar
Establish
MVP
Anudeep_Garnepudi
MVP

Likes

275 likes

Total Posts

320 posts

Correct reply

87 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile
Anudeep_Garnepudi
MVP

04-12-2020

Hi @Sravan_Kumar_Si 

I would suggest if you already have a support script(Sling Model or Use API), better write logic in the script itself like below. This will make you view simple ans clean.

Support Script:

model.title = title.contains("{0}") ? title.replace("{0}", currentPage.parent.title) : title; (JavaScript Use API example, you can try similar in Java Use API or Sling Model)

HTL:

<a>${title}</a>

-AG

Answers (3)

Answers (3)

Avatar

Avatar
Establish
MVP
Anudeep_Garnepudi
MVP

Likes

275 likes

Total Posts

320 posts

Correct reply

87 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile

Avatar
Establish
MVP
Anudeep_Garnepudi
MVP

Likes

275 likes

Total Posts

320 posts

Correct reply

87 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile
Anudeep_Garnepudi
MVP

04-12-2020

Hi @Sravan_Kumar_Si 

  1. We can not use options(@) in ternary operator which will results to an Exception (<a>${'{0}' in title ? title @ format=currentPage.parent.title : title}</a>)
  2. Even if you use if use it at the end, it will be ignored an @option will always execute <a>${!('{0}' in title) ? title : title @ format=currentPage.parent.title}</a>

If you want to do it from view(HTL), the best option is having two expressions

<a>${'{0}' in model.title ? model.title : "" @ format=currentPage.parent.title}  ${!('{0}' in model.title) ? model.title : ""}</a>

If you want your view(HTL) to be simple and more readable, as I mention earlier write the logic in support script.

Hope you got the Answer.

-AG

 

Avatar

Avatar
Springboard
Level 7
KiranVedantam1992
Level 7

Likes

198 likes

Total Posts

214 posts

Correct reply

61 solutions
Top badges earned
Springboard
Give Back 5
Ignite 1
Affirm 50
Validate 1
View profile

Avatar
Springboard
Level 7
KiranVedantam1992
Level 7

Likes

198 likes

Total Posts

214 posts

Correct reply

61 solutions
Top badges earned
Springboard
Give Back 5
Ignite 1
Affirm 50
Validate 1
View profile
KiranVedantam1992
Level 7

04-12-2020

Hi @Sravan_Kumar_Si,

 

Here is the working code:

 

<sly data-sly-test.title="${model.title}"/> 

<a>${'{0} in title' @ format= currentPage.getParent.getTitle ? currentPage.getParent.getTitle : title} </a>

 

Put the {0} and the other strings in single quotes.

 

Thanks,

Kiran Vedantam.

Avatar

Avatar
Give Back 3
MVP
Manjunath_K
MVP

Likes

140 likes

Total Posts

188 posts

Correct reply

61 solutions
Top badges earned
Give Back 3
Give Back
Boost 50
Boost 5
Boost 3
View profile

Avatar
Give Back 3
MVP
Manjunath_K
MVP

Likes

140 likes

Total Posts

188 posts

Correct reply

61 solutions
Top badges earned
Give Back 3
Give Back
Boost 50
Boost 5
Boost 3
View profile
Manjunath_K
MVP

04-12-2020

Hi @Sravan_Kumar_Si 

Please use the below mentioned format option syntax.

 

<sly data-sly-test.containsText="${'{0}' in title}">
     ${title @format = currentPage.parent.title}
</sly>

 

<sly data-sly-test="${!containsText}">
     ${title}
</sly>

 

Hope this helps!