Jezwin Varghese
Jezwin Varghese

Reputation: 325

How to append a character while using the conditional statement in sightly?

I need to set the href value depending on the nullity of sling:vanityPath. The hit.properties.sling:vanityPath returns the string value for sling:vanityPath property. I need to append a / before the hit.properties.sling:vanityPath value. Is it possible to do that in the following code or do I have to test it twice, but that comes with code repetition?

<a href="${hit.properties.sling:vanityPath == null? hit.path : hit.properties.sling:vanityPath @ extension='html'}"/>

Upvotes: 1

Views: 1360

Answers (3)

jmona789
jmona789

Reputation: 2839

You can use @ join to concat an array:

<a href="${hit.properties.sling:vanityPath ? ['/', hit.properties.sling:vanityPath] : hit.path @ join ='', 'extension='html'}"/>

Upvotes: 0

Ligia Luoana Barbanta
Ligia Luoana Barbanta

Reputation: 13

I don't think you can do it in one line. But you can try defining a variable for the concatenated value instead of duplicating code for the a tag. Also, do you have to check for null specifically? can't you just reverse the conditions and check if the value exists?

<sly data-sly-test.concatenatedURL="${['/', hit.properties.sling:vanityPath] @ join = ''}"/>
<a href="${hit.properties.sling:vanityPath ? concatenatedURL : hit.path @ extension='html'}"/>

Upvotes: 1

Vlad
Vlad

Reputation: 10780

Concatenation or binary operators are not supported in HTL. You can either use prependPath or double test for the slash.

Upvotes: 4

Related Questions