2

is it possible to use thymyleaf conditions for building javascript function ? For example I have a flag specialClient which I will pass from java code to template render engine. So now I want to write something like followed code in my template:

...
<th:if=${specialClient}>
callbackForSpecialCLient()
<else>
plainCallbackWithAdForPoorClients
<endif>
...

which (after rendering) should result in:

...
callbackForSpectialClient()
...

As you can see we don't have any if conditions in rendered result. That's what I want so much to achieve.

1 Answer 1

2

You could achieve your desired functionality with following code:

<script th:inline="javascript">

    <th:block th:if="${specialClient}">
        callbackForSpecialCLient();
    </th:block>

    <th:block th:unless="${specialClient}">
        plainCallbackWithAdForPoorClients();
    </th:block>

</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Ye I know this solution, but it force me to move callback call from method to another <script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.