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 an example of evaluated values passed to hx-headers. #2939

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

maciejtrybilo
Copy link

@maciejtrybilo maciejtrybilo commented Oct 3, 2024

Add an example of evaluated values passed to hx-headers.

Description

I haven't been clear from the documentation how to exactly have the javascript in the attribute evaluated as I'm not such a web dev expert. Perhaps it could help someone else if it's more explicit.

The example is more or less taken from my use case. Please feel free to suggest something else!

HTMX works great with Swift, BTW.

Checklist

  • I have read the contribution guidelines
  • I have targeted this PR against the correct branch (master for website changes, dev for
    source changes)
  • This is either a bugfix, a documentation update, or a new feature that has been explicitly
    approved via an issue
  • I ran the test suite locally (npm run test) and verified that it succeeded

Add an example of evaluated values passed to hx-headers.
@maciejtrybilo maciejtrybilo changed the title Update hx-headers.md Add an example of evaluated values passed to hx-headers. Oct 3, 2024
If you wish for `hx-headers` to *evaluate* the values given, you can prefix the values with `javascript:` or `js:`.

```html
<div hx-get="/example" hx-headers='{"myHeader": "My Value"}'>Get Some HTML, Including A Custom Header in the Request</div>
<body hx-headers='js:{ "Locale" : new Intl.DateTimeFormat().resolvedOptions().locale }'>...</body>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think to keep the example consistent, we could take inspiration from the hx-vals documentation, which has the following example:

<div hx-get="/example" hx-vals='{"myVal": "My Value"}'>Get Some HTML, Including A Value in the Request</div>

<div hx-get="/example" hx-vals='js:{myVal: calculateValue()}'>Get Some HTML, Including a Dynamic Value from Javascript in the Request</div>

I think it's a concise & effective approach, using the same key for the same context element, just changing how the value is retrieved and illustrating how js: works.

Let me know your thoughts on this! Your opinion will have more value than mine on this as I'm likely biased by my htmx experience (as you said, we want the docs to be clear enough for newcomers!)

@Telroshan Telroshan added the documentation Improvements or additions to documentation label Oct 4, 2024
@maciejtrybilo
Copy link
Author

@Telroshan Makes sense, I wasn't aware that the hx-vals page does have an example. And sorry for taking so long!

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

Successfully merging this pull request may close these issues.

2 participants