How to replace white space in HTL sightly? | Adobe Higher Education
Skip to main content
bhoang
Level 4
September 27, 2018
해결됨

How to replace white space in HTL sightly?

  • September 27, 2018
  • 9 답변들
  • 9455 조회

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

이 주제는 답변이 닫혔습니다.
최고의 답변: 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 답변

arunpatidar
Community Advisor
Community Advisor
September 27, 2018

Hi,

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

HTL Java Use-API

Arun Patidar
bhoang
bhoang작성자
Level 4
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

smacdonald2008
Level 10
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.

VeenaVikraman
Community Advisor
Community Advisor
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
Level 10
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.

bhoang
bhoang작성자
Level 4
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>

arunpatidar
Community Advisor
Community Advisor
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
raj_mandalapu
Level 7
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.

bhoang
bhoang작성자
Level 4
October 1, 2018

Thank you so much,