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

Add support for hx:vals for maps directly #27

Closed
checketts opened this issue Oct 20, 2022 · 7 comments
Closed

Add support for hx:vals for maps directly #27

checketts opened this issue Oct 20, 2022 · 7 comments
Assignees
Milestone

Comments

@checketts
Copy link
Collaborator

checketts commented Oct 20, 2022

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?

@checketts
Copy link
Collaborator Author

checketts commented Oct 20, 2022

An alternative option is to add a Formatter<Map<Any,Any>> like so:

    @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 {{ to trigger a formatter as well as the inline map creation like so:

hx:vals="${{ {id: id } }}"

I'm happy to document this approach in the README if that is the preferred option.

@wimdeblauwe
Copy link
Owner

I agree we should do something for hx:vals, using those &quot; things is making it very hard. But not sure what the best way forward is.

Does your first proposal mean that if you write hx:vals="${ {id: id } }" it would somehow automatically get rendered as hx-vals='{"id": 1234}' in the HTML ?

@wimdeblauwe
Copy link
Owner

Do you have a link to the Thymeleaf documentation about those inline maps ?

@checketts
Copy link
Collaborator Author

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.

@checketts
Copy link
Collaborator Author

Does your first proposal mean that if you write hx:vals="${ {id: id } }" it would somehow automatically get rendered as hx-vals='{"id": 1234}' in the HTML ?

Correct

@wimdeblauwe
Copy link
Owner

Seems like a good addition if you could do that.

@checketts
Copy link
Collaborator Author

Will do

@checketts checketts self-assigned this Oct 26, 2022
@wimdeblauwe wimdeblauwe added this to the 0.3.0 milestone Oct 27, 2022
wimdeblauwe added a commit that referenced this issue Nov 27, 2022
Addresses #27 adding support for maps the be serialized as json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants