Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Outputting a JS array outside th:inline script tag #980

Open
kg-currenxie opened this issue Nov 15, 2023 · 4 comments
Open

Outputting a JS array outside th:inline script tag #980

kg-currenxie opened this issue Nov 15, 2023 · 4 comments

Comments

@kg-currenxie
Copy link

Trying to output a string array into my DOM, but for our usage (js library) we need that in a HTML attribute like so

<div data-some-attr="{ aKey: ['hello', 'world'] }">

In this case, its just not possible to extract it to a <script th:inline> tag, nor is it convenient even if it worked.

what we want to do is ofc this:

<div th:data-some-attr="|{ aKey: ${model.myArray} }|">

obviously that gives

<div data-some-attr="{ aKey: [LStringArrayClass] }"> // or whatever

so then ofc im looking for some TH helpers

<div data-some-attr="{ aKey: ${#arrays.arrayJoin(model.myArray, ',')} }"> // or whatever

which gives

<div data-some-attr="{ aKey: hello,world }"> // WHAT??

No brackets, no quotes. How is this a string array?? (obviously #arrays.toStringArray doesn't help either, as it only converts to type)

Is there a way to solve this?

@duoduobingbing
Copy link

duoduobingbing commented Dec 1, 2023

This question is probably best asked on StackOverflow, as this is not a Thymeleaf bug. #arrays.arrayJoin joins an array into a String with a defined delimiter. In your case hello,world with the delimiter ,.

If you want single quotes and square brackets inside said String you will need to add them yourself.

Something like

<div th:data-some-attr="|{ aKey: [${'''' + #arrays.arrayJoin(model.myArray, ''',''') + '''' }] }|">

should probably work. Here is the documentation on how to escape a single quote in SpEL

@kg-currenxie
Copy link
Author

kg-currenxie commented Dec 1, 2023

Maybe this should be a feature request instead?

Tons of different technologies today use "templated javascript" driven by attributes, like htmx, vue, alpine etc..
None of these use inline script tags. Would it be so terrible to add a #arrays.toJSArray() ?

From thymeleaf.org:
image
image

i cant in any shape of form call this modern :P More like 2005..

<div th:data-some-attr="|{ aKey: [${'''' + #arrays.arrayJoin(model.myArray, ''',''') + '''' }] }|">

🤮

<div th:data-some-attr="|{ aKey: ${#arrays.toJSArray(model.myArray)} }|">

🤩

@duoduobingbing
Copy link

duoduobingbing commented Dec 1, 2023

If you have a Jackson ObjectMapper Bean inside your context (from e.g. Spring Boot); something like <div th:data-some-attr="|{ aKey: ${@objectMapper.writeValueAsString(model.myArray)} }|"> should also work.

Please also have a look at this Stackoverflow post and this one on how to get the ObjectMapper to output single instead of double quotes.

@kg-currenxie
Copy link
Author

We have used this as a workaround yes, but then we need to use JSON.parse() on the js side :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants