htmx

package
v0.35.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 21, 2026 License: AGPL-3.0 Imports: 2 Imported by: 0

README

HTMX Utilities

This package provides utility functions for working with HTMX requests in Go applications.

Functions

Request Detection
  • IsHtmx(r *http.Request) bool - Checks if the request is an HTMX request
  • IsHxBoosted(r *http.Request) bool - Checks if the request was boosted
  • IsHxHistoryRestoreRequest(r *http.Request) bool - Checks if it's a history restore request
  • IsHxRequest(r *http.Request) bool - Checks if it's an HTMX request (validates value is "true")
  • IsHxTrigger(r *http.Request) bool - Checks if the request was triggered by an event
Header Value Extraction
  • HxPrompt(r *http.Request) string - Gets the prompt message
  • HxTarget(r *http.Request) string - Gets the target element
  • HxTriggerName(r *http.Request) string - Gets the trigger name
CSS Utilities
  • HxHideIndicatorCSS() string - Returns CSS for hiding HTMX indicators

Usage

package main

import (
    "net/http"
    "github.com/dracory/base/htmx"
)

func handler(w http.ResponseWriter, r *http.Request) {
    if htmx.IsHtmx(r) {
        // Handle HTMX request
        target := htmx.HxTarget(r)
        trigger := htmx.HxTriggerName(r)
        
        // Process HTMX-specific logic
    }
    
    // Regular request handling
}

HTMX Headers Supported

  • HX-Request - Indicates an HTMX request
  • HX-Boosted - Indicates the request was boosted
  • HX-History-Restore-Request - Indicates a history restore request
  • HX-Trigger - Indicates the request was triggered by an event
  • HX-Trigger-Name - The name of the trigger
  • HX-Target - The target element
  • HX-Prompt - The prompt message

Dependencies

This package depends on the github.com/dracory/base/req package for header value extraction.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HxHideIndicatorCSS

func HxHideIndicatorCSS() string

HxHideIndicatorCSS returns the CSS for hiding the HTMX indicator.

Returns the CSS code for hiding the HTMX indicator.

Returns:

  • string: the CSS code

func HxPrompt

func HxPrompt(r *http.Request) string

HxPrompt gets the prompt message for the user.

Returns the value of the "HX-Prompt" header, or an empty string if the header is not present.

Parameters:

  • r: the http request

Returns:

  • string: the prompt message

func HxTarget

func HxTarget(r *http.Request) string

HxTarget gets the target element for the response.

Returns the value of the "HX-Target" header, or an empty string if the header is not present.

Parameters:

  • r: the http request

Returns:

  • string: the target element

func HxTriggerName

func HxTriggerName(r *http.Request) string

HxTriggerName gets the name of the event or trigger that initiated the request.

Returns the value of the "HX-Trigger-Name" header, or an empty string if the header is not present.

Parameters:

  • r: the http request

Returns:

  • string: the trigger name

func IsHtmx

func IsHtmx(r *http.Request) bool

IsHtmx checks if the given HTTP request is an HTMX request.

An HTMX request is determined by the presence of the "HX-Request" header.

Parameters:

  • r: the http request

Returns:

  • bool: true if it's an HTMX request, false otherwise

func IsHxBoosted

func IsHxBoosted(r *http.Request) bool

IsHxBoosted checks if the request has been boosted for performance.

Returns true if the "HX-Boosted" header is present and its value is "true", otherwise returns false.

Parameters:

  • r: the http request

Returns:

  • bool: true if the request has been boosted, false otherwise

func IsHxHistoryRestoreRequest

func IsHxHistoryRestoreRequest(r *http.Request) bool

IsHxHistoryRestoreRequest checks if the request is related to restoring the browser's history.

Returns true if the "HX-History-Restore-Request" header is present and its value is "true", otherwise returns false.

Parameters:

  • r: the http request

Returns:

  • bool: true if the request is a history restore request, false otherwise

func IsHxRequest

func IsHxRequest(r *http.Request) bool

IsHxRequest checks if this is an HTMX request.

Returns true if the "HX-Request" header is present and its value is "true", otherwise returns false.

Parameters:

  • r: the http request

Returns:

  • bool: true if it's an HTMX request, false otherwise

func IsHxTrigger

func IsHxTrigger(r *http.Request) bool

IsHxTrigger checks if the request was triggered by an event.

Returns true if the "HX-Trigger" header is present and its value is "true", otherwise returns false.

Parameters:

  • r: the http request

Returns:

  • bool: true if the request was triggered, false otherwise

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL