Reputation: 917
I am using Thymeleaf from Java application to generate my HTML emails. To replace the variables within the *.html template I am using following working syntax.
<p style="margin-top: 0px; ...;" th:inline="text">[(${myVariable})]</p>
The Java code example is following:
String mailContent = "myTemplateLoadedFromResourcesElsewhere";
// Context already filled with variables
org.thymeleaf.context.Context ctx = EMailGenerator.getContext();
String htmlMailResult = new SpringTemplateEngine().process(mailContent, ctx.getContext());
The problem is when some of the replacement is missing, there is just empty space.. I would like to either leave the name of variable (just not replaced at all), or throw some exception notifying me of missing information. Unfortunatelly I was not able to find similar options either in the Context or the org.thymeleaf.TemplateEngine
Upvotes: 3
Views: 301
Reputation: 21918
I think there are two very different questions here, so they should probably be asked as 2 separate SO questions.
Having said that:
1 Leaving the name of the variable
Inside of a [[...]]
or a [(...)]
inline expression, you can use the same types of Thymeleaf expressions that you would use inside th:text
or th:utext
.
So, you can do the following:
<p style="margin-top: 0px; ...;">[(${myVariable} ?: 'myVariable is missing!')]</p>
The ?:
operator will use the value on the right hand side of the ?:
, if the value on the left-hand side is null.
Given you are generating HTML content, however, you don't have to use these inline expressions at all. You can do this, if you prefer:
<p style="margin-top: 0px; ...;" th:text="${myVariable} ?: 'myVariable is missing!'"></p>
WARNING:
Never use [(...)]
or th:utext
if you will be handling user-provided (and therefore untrusted) inputs. Use [[...]]
or th:text
instead. This will ensure any HTML elements included in the inputs are safely encoded.
2 throw some exception notifying me of missing information
That is a potentially much bigger question, and depends on information not provided in the question, such as how you need to be notified, and how any data errors errors should affect the overall processing of your e-mail (or many e-mails).
Context already filled with variables
Using a validator such as Hibernate Validator is one way in which the fields of a bean could be checked before data is loaded into the template context. But that assumes you have a bean containing the related data.
Update: Using Inline Text
If you are using th:inline="text"
, the above examples are still valid.
But there is also an additional syntax which you can use - where expressions are placed between and opening [#
and a closing /]
. This is equivalent to using the th:block
element in standard HTML mode.
Example:
<div th:inline="text">
<p>
[# th:text="${myVariable} ?: 'myVariable is missing'" /]
</p>
</div>
In all these cases, however, you still need to be aware of escaped HTML vs. unescaped HTML, since this is all still taking place within the overall context of HTML processing, in your code.
There is more information about this additional syntax here.
Upvotes: 2