Table of Contents

Class HtmxHeadersTagHelper

Namespace
Ramstack.HtmxToolkit.TagHelpers
Assembly
Ramstack.HtmxToolkit.dll

Applies an hx-headers attribute assembled from hx-header-* attributes and the hx-all-headers dictionary.

[HtmlTargetElement(Attributes = "hx-headers-inherited")]
[HtmlTargetElement(Attributes = "hx-headers-append")]
[HtmlTargetElement(Attributes = "hx-all-headers")]
[HtmlTargetElement(Attributes = "hx-header-*")]
public sealed class HtmxHeadersTagHelper : TagHelper, ITagHelper, ITagHelperComponent
Inheritance
HtmxHeadersTagHelper
Implements
Inherited Members

Examples

@addTagHelper *, Ramstack.HtmxToolkit

<section hx-header-X-Tenant="@Model.TenantId">
    <button hx-get="/reports" hx-target="#report">Load report</button>
</section>

@* Renders hx-headers='{"X-Tenant":"..."}' with the JSON built and escaped for you. *@

Remarks

  • HTMX 1.9.x and 2.x merge-inherit hx-headers from parent elements.
  • HTMX 4.x requires either the explicit inheritance modifier or the global ImplicitInheritance option for inheritance.
  • In HTMX 4.x, the append modifier merges a child declaration into inherited headers.

An InvalidOperationException is thrown if the element already has an hx-headers attribute or its alias. The default alias is data-hx-headers; Prefix determines the alias in HTMX 4.x. The check also covers attributes with HTMX 4.x modifiers, such as hx-headers:inherited.

Constructors

HtmxHeadersTagHelper(IOptions<HtmxToolkitOptions>)

Applies an hx-headers attribute assembled from hx-header-* attributes and the hx-all-headers dictionary.

public HtmxHeadersTagHelper(IOptions<HtmxToolkitOptions> options)

Parameters

options IOptions<HtmxToolkitOptions>

Remarks

  • HTMX 1.9.x and 2.x merge-inherit hx-headers from parent elements.
  • HTMX 4.x requires either the explicit inheritance modifier or the global ImplicitInheritance option for inheritance.
  • In HTMX 4.x, the append modifier merges a child declaration into inherited headers.

An InvalidOperationException is thrown if the element already has an hx-headers attribute or its alias. The default alias is data-hx-headers; Prefix determines the alias in HTMX 4.x. The check also covers attributes with HTMX 4.x modifiers, such as hx-headers:inherited.

Properties

Append

Gets or sets a value indicating whether hx-headers is appended to inherited headers.

[HtmlAttributeName("hx-headers-append")]
public bool Append { get; set; }

Property Value

bool

Remarks

The property does not change the generated attribute name for HTMX 1.9.x and 2.x.

For HTMX 4.x, the tag helper emits the append modifier when this property is true.

Headers

Gets or sets the hx-headers attribute values, which can also be set with hx-header-* attributes.

[HtmlAttributeName("hx-all-headers", DictionaryAttributePrefix = "hx-header-")]
public IDictionary<string, string> Headers { get; set; }

Property Value

IDictionary<string, string>

Remarks

The default dictionary compares header names case-insensitively.

Inherited

Gets or sets a value indicating whether hx-headers is explicitly inherited.

[HtmlAttributeName("hx-headers-inherited")]
public bool Inherited { get; set; }

Property Value

bool

Remarks

HTMX 1.9.x and 2.x merge-inherit headers without a modifier, so the property does not change the generated attribute name for them.

For HTMX 4.x, the tag helper emits the inherited modifier when this property is true.

Methods

ProcessAsync(TagHelperContext, TagHelperOutput)

Asynchronously executes the TagHelper with the given context and output.

public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)

Parameters

context TagHelperContext

Contains information associated with the current HTML tag.

output TagHelperOutput

A stateful HTML element used to generate an HTML tag.

Returns

Task

A Task that on completion updates the output.

Remarks

By default this calls into Process(TagHelperContext, TagHelperOutput).