Expand my Community achievements bar.

SOLVED

How to replace white space in HTL sightly?

Avatar

Level 4

Hi all,

I want to replace white space in HTL sightly.

Example: The sectionTitle is "I love you", I want the data-taget is "I-love-you"

<ul data-sly-list="${contents.sections}">

     <li class="title">

          <a data-target="#${item.sectionTitle}" class="links">${item.sectionTitle}</a>

     </li>

</ul>

Please help me, How to do that?

Thank you so much,

BienHV

1 Accepted Solution

Avatar

Correct answer by
Level 8

The code which you mentioned works only on static content, and the ternary condition is sufficient out here to fulfill your requirement.

<a data-target="#${item.sectionTitle=='i love you' ? 'i-love-you' : (item.sectionTitle=='i hate you' ? 'i-hate-you' : itemList.index[0])}" class="links">${item.sectionTitle}</a>

But, what you are expecting is replace functionality which is not possible with sightly, so we prefer to use Java code. if the content is dynamic and you don't know what content author is going to enter in the section title field then you must write Java code. you can use WCMUsePojo or sling models.

View solution in original post

9 Replies

Avatar

Community Advisor

Hi,

You can use JAVA to achieve this. Please check below:

HTL Java Use-API



Arun Patidar

Avatar

Level 4

Thank you for your help.

I am tried this code:

<a data-target="#${item.sectionTitle=='I love you' ? 'i-love-you' : itemList.index[0]}" class="links">${item.sectionTitle}</a>

It worked.

But I want more a conditon, example:

if(title=='I love you') print 'i-love-you' elseif title='i-hate-you' print 'i-hate-you' else print 'no-thing'

How to apply it on sightly same as the code above?

Thank you so much,

BienHV

Avatar

Level 10

See this thread - Sightly remove whitespace

If you need to remove white space - then use Java USE API as Arun suggests. One of the benefits of using Java - you have the full functionality of Java built into your component.

Avatar

Community Advisor

Same recommendation, in your sling Model @postconstruct method you can get this value and do the logic you want to handle and assign the sectionTitle as the final value you want to return

Avatar

Level 10

ALl of these answers are suggesting that you use Java - either with Sling Models or WCMUsePojo to perform string manipulations like this use case.

Avatar

Level 4

Hi,

I used the code as below. it work for me.

<a data-target="#${item.sectionTitle=='i love you' ? 'i-love-you' : (item.sectionTitle=='i hate you' ? 'i-hate-you' : itemList.index[0])}" class="links">${item.sectionTitle}</a>

Avatar

Community Advisor

Hi,

Your example will work if you know the exact set of strings which has to be replaced but if sectionTitle is unknown you can't compare.

Better to go with Java and make a generic solution.



Arun Patidar

Avatar

Correct answer by
Level 8

The code which you mentioned works only on static content, and the ternary condition is sufficient out here to fulfill your requirement.

<a data-target="#${item.sectionTitle=='i love you' ? 'i-love-you' : (item.sectionTitle=='i hate you' ? 'i-hate-you' : itemList.index[0])}" class="links">${item.sectionTitle}</a>

But, what you are expecting is replace functionality which is not possible with sightly, so we prefer to use Java code. if the content is dynamic and you don't know what content author is going to enter in the section title field then you must write Java code. you can use WCMUsePojo or sling models.