Readability of thymeleaf
Readability of thymeleaf
I'm used to writing templates like this:
${name}
This seems much more readable for me, in that I can see the variables outside of the tags themselves. Currently in thymeleaf, I would need to do:
Or:
${name} // this text will be replaced and doesn't matter
Is there a way to enter in the variable like I have at the top?
P.S. worth reading
– Boris the Spider
Jul 2 at 21:13
If you want it that way go with JSP and the Standard Tag Library.At the moment you are ordering beef that should taste like fish.
– Flocke
Jul 3 at 6:45
1 Answer
1
Sure, you can use expression inlining for this.
[[${name}]]
In the documentation I linked, it does have a discussion of using expression inlining vs natural templating. I personally prefer natural templating in most cases.
Eugh - that's heinous. Seems like the very definition of "worst of both worlds". This is meant for very rare situations where the standard syntax doesn't suffice - it's absolutely not meant to be an alternative syntax.
– Boris the Spider
Jul 2 at 21:14
If you are using the latest thymeleaf -- 3.0 at the time of this, it does it automatically. If you are using 2.0, you probably have to put
th:inline="text"
in your tag.... which is even uglier.– Metroids
Jul 2 at 21:17
th:inline="text"
@David542 if this makes you happy please just use Freemarker. Don't abuse Thymeleaf this way - it'll make people cry.
– Boris the Spider
Jul 2 at 21:17
@David542 you would put some text in the
div
, that's the whole point! You create a page that looks like it should when viewed without rendering. John Smith
for example.– Boris the Spider
Jul 2 at 21:23
div
@David542 -- if you want to go completely the thymeleaf way, it should look like this:
David542
. That way when you open it in the browser (without a server), it looks like it's working (e.g. it looks like ${name}
is replaced with David542). And when you run it through a server where the thymeleaf is interpreted, David542 is actually replaced with ${name}
. However, in practice I always write
just because it's more succinct.– Metroids
Jul 2 at 21:26
${name}
${name}
<div th:text="${name}" />
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
The point of Thymeleaf is that templates are valid and renderable HTML, this allows for easier 1) styling, 2) validation, 3) formatting. Personally I think that Thymelead is far more readable than mixing multiple different languages. Now - the catch - the "I think" means that this is opinion based, so again off topic.
– Boris the Spider
Jul 2 at 21:10