responsetargets

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 19, 2024 License: 0BSD Imports: 4 Imported by: 0

Documentation

Overview

package responsetargets allows you to specify different target elements to be swapped when different HTTP response codes are received.

Index

Examples

Constants

View Source
const Error errorCode = "error"

Error is a status code that covers all 4xx and 5xx HTTP response codes.

View Source
const Extension htmx.Extension = "response-targets"

Extension allows you to specify different target elements to be swapped when different HTTP response codes are received.

Install

<script src="https://unpkg.com/htmx.org@1.9.12/dist/ext/response-targets.js"></script>

Extension: response-targets

Variables

This section is empty.

Functions

func Target

func Target[T any](hx htmx.HX[T], code Code, extendedSelector htmx.TargetSelector) T

Target specifies a target element to be swapped when specific HTTP response codes are received.

Extension: response-targets

Example (Code)
package main

import (
	"fmt"
	"net/http"

	"github.com/will-wow/typed-htmx-go/htmx"
	"github.com/will-wow/typed-htmx-go/htmx/ext/responsetargets"
)

var hx = htmx.NewStringAttrs()

func main() {
	attr := responsetargets.Target(hx, responsetargets.Status(http.StatusNotFound), htmx.TargetRelative(htmx.Next, "div"))
	fmt.Println(attr)
}
Output:

hx-target-404='next div'
Example (Error)
package main

import (
	"fmt"

	"github.com/will-wow/typed-htmx-go/htmx"
	"github.com/will-wow/typed-htmx-go/htmx/ext/responsetargets"
)

var hx = htmx.NewStringAttrs()

func main() {
	attr := responsetargets.Target(hx, responsetargets.Error, htmx.TargetThis)
	fmt.Println(attr)
}
Output:

hx-target-error='this'
Example (Wildcard)
package main

import (
	"fmt"

	"github.com/will-wow/typed-htmx-go/htmx"
	"github.com/will-wow/typed-htmx-go/htmx/ext/responsetargets"
)

var hx = htmx.NewStringAttrs()

func main() {
	attr := responsetargets.Target(hx, responsetargets.Wildcard(4, 0), htmx.TargetRelative(htmx.Next, "div"))
	fmt.Println(attr)
}
Output:

hx-target-40*='next div'
Example (WildcardX)
package main

import (
	"fmt"

	"github.com/will-wow/typed-htmx-go/htmx"
	"github.com/will-wow/typed-htmx-go/htmx/ext/responsetargets"
)

var hx = htmx.NewStringAttrs()

func main() {
	attr := responsetargets.Target(hx, responsetargets.WildcardX(4, 0), htmx.TargetRelative(htmx.Next, "div"))
	fmt.Println(attr)
}
Output:

hx-target-40x='next div'

func Wildcard

func Wildcard(digits ...int) wildcard

Wildcard creates a wildcard code with the given digits. For example, Wildcard(4, 1) results in hx-target-41*, and matches all 41x HTTP response codes.

func WildcardX

func WildcardX(digits ...int) wildcardX

WildcardX creates a wildcard code with the given digits, and uses an 'x' instead of a '*' in the generated attribute. For example, WildcardX(4, 1) results in hx-target-41x, and matches all 41x HTTP response codes.

Types

type Code

type Code interface {
	// contains filtered or unexported methods
}

A Code is a complete or partial HTTP response code.

type Status

type Status int

A Status is a complete HTTP response code. You can wrap the http.Status* constants with Status.

Jump to

Keyboard shortcuts

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