Spring Boot + Freemarker master template - spring-boot

I`m trying to build an app with spring boot and freemarker as template engine. The problem I have is, I want to make a "master template" for all my pages to use. I found out that this is achievable in Freemarker with macros. This is how my indexmaster.ftl looks like:
[#macro indexmaster title="defaultTitle"]
<html>
<head> css stuff </head>
<body>
<div id="content">[#nested /]</div>
</body>
</html>
[/#macro]
and in the other pages, I use the macro like this:
[#import "/WEB-INF/ftl/master/indexmaster.ftl" as layout /]
[#layout.indexmaster title="My title"]
...rest of the page
[/#layout.indexmaster]
The problem I`m facing is, the freemarker "code" is interpreted as text when I access the page
click me
What am I doing wrong? Is there any extra spring boot configuration needed?

Use "<>" instead of "[]" for Freemarker tags.

You need to set the tag_syntax configuration setting of FreeMarker to auto_detect or square_bracket. The default is angle_bracket for backward compatibility. Another option is to start the template with [#ftl], which turns on square bracket syntax even if tag_syntax is angle_bracket.

Related

Thymeleaf don't expand data-<name> attribute on body tag

I have to downgraded a project with Spring Boot + Thymeleaf to make it work on Tomcat 7 for reasons of the production environment.
I have a Thymelead template where I collect some variables on HTML body tag:
<body th:title="${key}" th:data-key="${key}" th:data-rol="${rol}" th:data-iden="${iden}">
The title attribute was introduced to see that the variable 'key' is collected.
In the original project with Thymeleaf 3.0 (running on Tomcat 8.5) all the variables was collected but with the downgrade to Thymeleaf 2.1.6 only the attribute title is initialized.
Is there any possibility to make the template works with this approach?
I doubt there is a way to turn on that functionality in Thymeleaf 2, that being said the alternative syntax would be this (should work in all versions):
<body th:title="${key}" th:attr="data-key=${key},data-rol=${rol},data-iden=${iden}">

Why can't I get Thymeleaf to work on Script tags

I have a simple Spring Boot application and I am trying to add Thymeleaf. The basic structure of my app is available here.
Basically when I try to render the following in the template...
<h1>[[${key}]]</h1>
It works perfect! However, when I try...
<script src="https://my.api.com/api/js?key=${key}" />
The ${key} is not getting replaced. What am I missing?
If you want Thymeleaf to resolve attributes, you have to prefix them with th:.
<script th:src="|https://my.api.com/api/js?key=${key}|" />
or
<script th:src="#{https://my.api.com/api/js(key=${key})}" />

How to modify index.html in runtime from Controller in Spring Boot?

I am working on a Spring Boot project which basically serving the UI from resources/public. When the request for the main page is arrived, I want to modify the index.html to add some data.
How can I do that?
EDIT: I forgot to mention that, the UI is written with AngularJS,HTML & SCSS if this changes anything. And I don't want to change variables, just want to add some data in <script> tag.
You can use a template engine such as Thymeleaf, Velocity, Freemarker...
In your Controller fill your model :
model.addAttribute("name", name);
In your Thymeleaf template :
<p th:text="'Hello, ' + ${name} + '!'" />
Here's an example with Thymeleaf :
https://spring.io/guides/gs/serving-web-content/

Thymeleaf th:include doesn't work after update to Spring Boot 1.3

I have an application which works fine with Spring Boot 1.2.6. Now I tried to 1.3.5 and have the problem, that the following statement doesn't work anymore:
<head >
<title th:text="#{app.title} + ' - ' + #{login.title}"></title>
<th:block th:include="main::head"/>
</head>
I can see in the Thymeleaf log that the main.html is found. Furthermore the Thymeleaf-Expressions from the head are evaluated. How ever the html in the browser has no content in head, neither title nor the content from main.html.
As far as I can see the Thymeleaf version hasn't changed.
So what can be the reason?
The version of the Layout dialect has changed. So I added the property <thymeleaf-layout-dialect.version>1.2.9</thymeleaf-layout-dialect.version> to my pom.xml and everything was fine.
I created an issue for that.

Display Spring MVC model attributes in thymeleaf template

I am developing a full Spring application with Spring MVC and Thymeleaf in view layer. In the past I've worked with JSPs and Spring MVC in view layer, but those are now dinosaurs I guess.
So my problem is that with JSPs I could very easily display model attributes in view by adding value in model.addAttribute in controller and displaying the same in JSP anywhere with placeholder evaluating to springex ${value}. So if I want to place a title in page I can write <title>${appName}<title>. This is one of the places where I can put any springex.
I am having hard time to figure out how to do this with Thymeleaf as it uses attribute based parsers. So anywhere on page if thymeleaf prefix is not included it won't process spring expression. It's very hard to work with limited set of tag libraries. I've heard of custom attributes for thymeleaf but I guess there should be a better way to do this.
You can use the th:text attribute, e.g.
<html ... xmlns:th="http://www.thymeleaf.org">
...
<title th:text="${appName}">mocking text</title>
...
</html>
The content of the tag ("mocking text" in this case) gets replaced by the result of the expression in the th:text attribute.
Of course you need to have the appropriate JAR files on CLASSPATH and have the Thymeleaf view resolver properly configured, as described in the Thymeleaf+Spring guide.
For additional information about how template processing works with Thymeleaf in general you can refer to the Thymeleaf guide.

Resources