How to concatenate strings in Sightly/HTL?
Asked Answered
M

2

11

I have the following code:

<sly data-sly-use.link="${'core.impl.view.tools.LinkUtils' @ path=properties.targetURL}"></sly>

I want to concatenate properties.linkType to properties.targetURL.

Any ideas how it can be done? I've found examples on the net but they don't seem to work for my situation.

Mesenchyme answered 8/5, 2017 at 6:45 Comment(0)
S
22

That depends on what kind of string concatenation you have in mind:

  1. Concatenating strings using an operator is not supported, ie. you cannot do ${properties.targetURL + properties.linkType}. A workaround (suggested by @Jens) is to do something like: <sly data-sly-test.concatenated="${'{0}{1}' @ format=[properties.targetURL, properties.linkType]}"></sly>
  2. Concatenating strings in HTML output can be done by placing HTL expression next to each other, ie. ${properties.targetUrl}${properties.linkType}
  3. Sending both strings to an Use Object is supported via multiple expression options: <sly data-sly-use.link="${'core.impl.view.tools.LinkUtils' @ path=properties.targetURL, type=properties.linkType}"></sly>
  4. Concatenating strings to form an URL might be possible in some cases using URI Manipulation
Superiority answered 8/5, 2017 at 8:18 Comment(3)
There is another option which is not really useful but for sake of completeness: ${'{0}{1}' @ format=[properties.string1, properties.string2]}. See github.com/Adobe-Marketing-Cloud/htl-spec/blob/master/…Macedo
@Jens, that is very creative especially if combined with data-sly-testSuperiority
I was thinking about a workaround using data-sly-test as well, but I am not sure you can use format with data-sly-test. If so, you could create a new concatenated string which in turn can be used in the statement the ops wants to use it in.Macedo
A
14

I just want to add one more way to concatenate strings to the above answer, by using @ join.

<sly data-sly-test="${['String1','String2','String3'] @ join = '-'}"/>

It will give output as: String1-String2-String3

Advertence answered 12/9, 2017 at 7:11 Comment(2)
Can you send me full syntax, what you are writingAdvertence
looks like it was my mistake, I was using wrong context, that's why this join = ' wasn't working, so I take it back what I wrote, everything in okLudwigg

© 2022 - 2024 — McMap. All rights reserved.