NAME

Uniform::HTMX::PSGI - Explicit framework-agnostic htmx adapter for the PSGI/Plack ecosystem

SYNOPSIS

use Uniform::HTMX::PSGI;

my $app = sub {
    my $env  = shift;
    my $htmx = Uniform::HTMX::PSGI->new($env);

    # Inspect incoming htmx request attributes
    if ($htmx->is_htmx) {
        my $target = $htmx->target;

        # Queue outbound htmx response headers
        $htmx->res_trigger('itemSaved', { id => 42 })
            ->res_reswap('innerHTML');
    }

    my $res = [ 200, [ 'Content-Type' => 'text/html' ], [ '<div>Saved!</div>' ] ];

    # Inject queued headers into the PSGI response
    return $htmx->apply($res);
};

DESCRIPTION

Uniform::HTMX::PSGI provides a seamless integration between the Uniform::HTMX base class and the PSGI specification (and by extension, the Plack middleware ecosystem).

It automatically translates PSGI $env variables (like HTTP_HX_REQUEST) into the standard htmx request headers, and provides an apply method capable of injecting htmx response headers into standard PSGI array references or response objects.

METHODS

new( $env )

Constructs a new Uniform::HTMX::PSGI object. Accepts the standard PSGI $env hashref.

apply( $res )

Injects any queued htmx outbound headers into the given response. $res can be:

  • A standard PSGI array reference: [ $status, \@headers, \@body ]

  • An object with a headers method.

  • An object with a header method.

Returns the modified $res for easy chaining.

SEE ALSO

Uniform::HTMX

PSGI