How to replace white space in HTL sightly? | Community
Skip to main content
bhoang
New Participant
September 27, 2018
Solved

How to replace white space in HTL sightly?

  • September 27, 2018
  • 9 replies
  • 9443 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by raj_mandalapu

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.

9 replies

bhoang
bhoangAuthor
New Participant
October 1, 2018

Thank you so much,

raj_mandalapu
raj_mandalapuAccepted solution
New Participant
September 28, 2018

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.

arunpatidar
New Participant
September 28, 2018

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
bhoang
bhoangAuthor
New Participant
September 28, 2018

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>

smacdonald2008
New Participant
September 27, 2018

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

VeenaVikraman
New Participant
September 27, 2018

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

smacdonald2008
New Participant
September 27, 2018

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.

bhoang
bhoangAuthor
New Participant
September 27, 2018

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

arunpatidar
New Participant
September 27, 2018

Hi,

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

HTL Java Use-API

Arun Patidar