bigskysoftware / htmx

</> htmx - high power tools for HTML
https://htmx.org
Other
37.84k stars 1.28k forks source link

[Feature] "hx-include" element as html equivalent to script #2007

Open VictorSohier opened 10 months ago

VictorSohier commented 10 months ago

hx-include

Who

Here are the kinds of people I imagine using this feature:

What

The idea is that this would be an element working in the style of #include <> in C (i.e., a simple replace), likely looking something along the lines of <hx-include src="x.html"></hx-include>, mirroring <script src="x.js"></script>.

Why

Webcomponents are a good idea, but they are half baked. It never seemed like anyone put any thought in their usability and reusability. Currently, if you want to use a webcomponent, you have two options:

How

Using a webcomponent to solve webcomponent problems.

If the maintainers of htmx have additional questions before implementation begins, please ask. It will help flesh out this idea or kill it if warranted.

qrhfz commented 10 months ago

hx-include is already used https://htmx.org/attributes/hx-include/

svenberkvens commented 10 months ago

Maybe call it <hx-load/> or <hx-replace/> instead?

This would be the first actual tag that HTMX would support. All other HTMX's functionality is invoked by attributes on normal HTML tags. So it would be "different". That requires good documentation.

Another idea would be:

<script type="hx-load" src="x.html"/>

But that comes close to what you were trying to avoid.

VictorSohier commented 10 months ago

Here is an existing implementation as something seperate. If the maintainers want it, I can make a PR: https://github.com/VictorSohier/html-include

svenberkvens commented 10 months ago

That looks interesting :-) Let's see if the devs are interested!

wmanley commented 10 months ago

Under what circumstances would you choose this approach rather than doing the include entirely server-side using the templating system of your choice?

The only thing I can come up with is if you have parts of your page that are going to be slow to load, so you want to return the bulk of your HTML first, and then afterwards show a spinner while a particular value is being generated, finally swapping it in. hx-trigger="load" seems fine in those cases.

svenberkvens commented 10 months ago

At my company, we have two backends. One legacy one and one modern one. The legacy backend embeds some elements in the HTML that loads stuff from the modern backend. For example, the modern backend is much better at generating active message counters than the legacy backend. So we let the frontend fetch those counters from the modern backend. The legacy backend does not do anything with the message counters (anymore).

The above is another use case for the proposal, although we currently handle it with hx-trigger="load".