Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate strings in Sightly/HTL?

Tags:

sightly

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.

like image 252
mrjayviper Avatar asked May 08 '17 06:05

mrjayviper


People also ask

What is the best way to concatenate strings in C#?

Concatenate String Using + Operator The simplest method of adding two strings in C# is using + or += operators.

Can you concatenate strings in C#?

You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time.

What character is used for concatenation in C #?

C++ '+' operator for String Concatenation C++ '+' operator can be used to concatenate two strings easily. The '+' operator adds the two input strings and returns a new string that contains the concatenated string.


2 Answers

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

like image 87
Manisha Avatar answered Sep 28 '22 02:09

Manisha


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
like image 20
Vlad Avatar answered Sep 28 '22 04:09

Vlad