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
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

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

Sravan_Kumar_Si
Level 2
Level 2

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?

1 Accepted Solution
Anudeep_Garnepudi
Correct answer by
Community Advisor
Community Advisor

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

View solution in original post

12 Replies
Manjunath_K
Community Advisor
Community Advisor

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!

Manjunath_K
Community Advisor
Community Advisor

@Sravan_Kumar_Si 

please try adding conditions as mentioned below.

 

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

 

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

 

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

 

 

 

 

Kiran_Vedantam
Community Advisor
Community Advisor

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.

Sravan_Kumar_Si
Level 2
Level 2
Hi Kiran, Thanks for getting back but this did not solve my issue. '{0}' in title - here, I want to check for the existence of substring '{0}' in value of title variable. If I enclose every thing inside '' then "in" is not considered as an operator and "title" is not taken as value of the variable.
Sravan_Kumar_Si
Level 2
Level 2

Hi @Kiran_Vedantam  and @Manjunath_K , Thanks for getting back but this did not solve my issue. My objective is if the place holder {0} is there in the value of title then replace it otherwise simple use the value of title. '{0}' in title - this is to check for the existence of substring '{0}' in value of title variable. If I enclose every thing inside '' then "in" is not considered as an operator and "title" is not taken as value of the variable.

Kiran_Vedantam
Community Advisor
Community Advisor

Hi to replace and use parentpageTitle instead of title you can use the below code:

 

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

 

I am not sure why are you using @ format= here.

 

Thanks,

Kiran Vedantam

Sravan_Kumar_Si
Level 2
Level 2

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

My objective is if the place holder {0} is there in the value of title then replace it otherwise simple use the value of title.

Anudeep_Garnepudi
Correct answer by
Community Advisor
Community Advisor

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

View solution in original post

Sravan_Kumar_Si
Level 2
Level 2
Thanks for your suggestion. But I wanted to know why the way I tried did not work?
Sravan_Kumar_Si
Level 2
Level 2
Hi Anurag, I had a solution as you mentioned. In my case, title comes from a different model only to fetch from dictionary. I was trailing for a short and simple HTL expression that does the job instead of writing a model for one line functionality.
Anudeep_Garnepudi
Community Advisor
Community Advisor
Please check my other comment with one line HTL solution
Anudeep_Garnepudi
Community Advisor
Community Advisor

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