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
Solved! Go to Solution.
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
Views
Replies
Total Likes
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
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
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
Views
Replies
Total Likes
ALl of these answers are suggesting that you use Java - either with Sling Models or WCMUsePojo to perform string manipulations like this use case.
Views
Replies
Total Likes
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>
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
Thank you so much,
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies