[UPDATED: combined a bunch of comments below into a general description/issue].
Htmxor needs a good way to do "template fragments". The library currently have a very rough first attempt, <HtmxPartial>
. It may need a better name and certainly more features.
Let me start by explaining how <HtmxPartial>
works right now.
Lets use this as the example:
@page "/counter"
<PageTitle>Counter</PageTitle>
<div id="counter">
<HtmxPartial>
<p role="status">
Current count: @CurrentCount
</p>
<button class="btn btn-primary"
hx-put="/counter"
hx-vals='{ "CurrentCount": @(CurrentCount) }'
hx-target="#counter"
@onput="IncrementCount">
Click me
</button>
</HtmxPartial>
</div>
@code {
[SupplyParameterFromForm]
private int CurrentCount { get; set; } = 0;
private void IncrementCount(HtmxContextEventArgs args)
{
CurrentCount++;
}
}
Normal request or hx-boosted request
During a normal request or hx-boosted request, the full component tree is rendered, i.e. starting with App.razor
, all the way down to the Counter.razor
page.
Here the full component tree is rendered out, including <HtmxPartial>
and its child content.
hx-request to /counter
When a hx-request is received that targets the component, that component's component-tree is searched, and since a component is found whose [Parameter] public bool Condition { get; set; } = true;
returns true, only that <HtmxPartial>
component and its children is rendered.
Normal request or htmx request
During rendering, if a component is reached, its Condition
parameter is checked, and if it returns false
, the component and its children is not rendered.
This happens during both normal and htmx requests.
A more detailed discussion of the proposed improvements is in the comment #25 (comment) below.