-
Notifications
You must be signed in to change notification settings - Fork 49
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
Add support for hx:vals for maps directly #27
Comments
An alternative option is to add a @Bean
fun mapFormatter(mapper: ObjectMapper) : org.springframework.format.Formatter<Map<Any,Any>> {
return MapFormatter(mapper)
}
class MapFormatter(private val mapper: ObjectMapper) : org.springframework.format.Formatter<Map<Any,Any>> {
override fun print(obj: Map<Any, Any>, locale: Locale): String {
return mapper.writeValueAsString(obj)
}
override fun parse(text: String, locale: Locale): Map<Any, Any> {
return mapper.readValue(text)
}
} Then use the double curly hx:vals="${{ {id: id } }}" I'm happy to document this approach in the README if that is the preferred option. |
I agree we should do something for Does your first proposal mean that if you write |
Do you have a link to the Thymeleaf documentation about those inline maps ? |
It appears to be a SPEL feature (https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#expressions-inline-maps) I haven't found anything in Thymeleaf's documetation for it yet. |
Correct |
Seems like a good addition if you could do that. |
Will do |
Addresses #27 adding support for maps the be serialized as json
hx-vals
allows for json data to be added to a request. However that means it needs to be escaped with all the correct quotes like so:hx:vals="${'{"id":"' + myId + '"}'}"
which is rendered as:
hx-vals="{'id': 1234}"
but is very hard to write and read.Thymeleaf supports inline maps like so:
hx:vals="${ {id: id } }"
which renders as
hx-vals="{id: 1234}"
(note the lack of quotes for the attribute name) Which is not proper JSON and htmx can't use it. I would like to propose adding support for rendering JSON (using Jackson) for hx-vals.What do you think?
The text was updated successfully, but these errors were encountered: