12

I'm trying to append some text to a dynamic text as shown below:

<label th:text="Hello ${worldText}"></label>

But the UI throws:

TemplateProcessingException: Could not parse as expression: "Hello ${worldText}

Does anyone know how can I achieve this?

Benjamin Schüller
  • 2,104
  • 1
  • 17
  • 29
Vasudev
  • 803
  • 1
  • 7
  • 16

3 Answers3

33

An easy solution would be to insert a span to the label:

<label>Hello <span th:text="${worldText}"></span></label>

But i would prefer to combine text and variables like this:

<label th:text="'Hello' + ${worldText}"></label>
Benjamin Schüller
  • 2,104
  • 1
  • 17
  • 29
  • Worked for me. Former seems like a work around, but latter suites well in my case, I have some more things to append in the text. Thanks :) – Vasudev Sep 08 '17 at 14:59
  • Seems `text` is not an official attribute for ` – Manuel Jordan Dec 23 '20 at 16:03
3

Another straightforward solution is

<label th:text="${'Hello ' + worldText}"></label>
michaeak
  • 1,548
  • 1
  • 12
  • 23
1

Some other ways,

// 1. Using the <th:block> element
<label>Hello <th:block th:text="${worldText}"></th:block></label>

// 2. Using string concatenation
<label th:text="${'Hello ' + worldText}"></label>

// 3. Using the pipe (|) character
<label th:text="|Hello ${worldText}|"></label>

// 4. Using expression inlining
<label>Hello [[${worldText}]]</label>

// 5. You could prepare a variable in your controller
// In contoller,
model.addAttribute("helloWorld", "Hello " + worldText);
// In template,
<label th:text="${helloWorld}"></label>

The result will always be,

<label>Hello variable-value</label>
Georgios Syngouroglou
  • 18,813
  • 9
  • 90
  • 92