Conditional text in ThymeLeaf : how to do it in plain text?
Asked Answered
I

2

6

I understand how th:if works for html templates, but I don't find any clue on how to do it when you expect plain text (use case : plain text e-mail templating).

So far I tried :

<html xmlns:th="http://www.thymeleaf.org" th:inline="text" th:remove="tag">
  Dear [[${contact.firstname}]] [[${contact.lastname}]],
  An alert was triggered at location:  [[${account.address}]] 
  <span th:if=\"${videoLink}\">To view your security camera recordings, please click on [[${videoLink]]</span>
</html>

It works... but the result contains the tag. Any idea of what I'm doing wrong ?

Thanks, Cyril

Inscrutable answered 3/6, 2015 at 10:5 Comment(0)
D
9

Thymeleaf 2.1 has a th:block tag, which is basically a container for attributes. Your conditional text can be done like this:

<th:block th:if="${videoLink}">To view your...</th:block>
Dramaturge answered 3/6, 2015 at 13:42 Comment(2)
That's exactly what I was looking for, thank you. I replaced all my <span th:remove="tag"> with <th:block> .Inscrutable
Thank you for your time and effortSlumberland
I
3

It seems that, unlike the th:inline, the th:remove="" is NOT applied to child nodes and has to be added for each tag. Here, if I add it to the tag, the result is what I wanted :

<html xmlns:th="http://www.thymeleaf.org" th:inline="text" th:remove="tag">
  Dear [[${contact.firstname}]] [[${contact.lastname}]],
  An alert was triggered at location:  [[${account.address}]] 
  <span th:if=\"${videoLink}\" th:remove="tag">To view your security camera recordings, please click on [[${videoLink]]</span>
</html>
result :

Dear John Doe,
An alert was triggered at localation: 205 North Michigan Avenue Chicago, IL

To view your security camera recordings, please click on http://www.video.com?id=007   
Inscrutable answered 3/6, 2015 at 13:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.