diff options
| author | Xe Iaso <me@xeiaso.net> | 2024-08-21 14:10:15 -0400 |
|---|---|---|
| committer | Xe Iaso <me@xeiaso.net> | 2024-08-21 14:20:44 -0400 |
| commit | 39660d978304ebce08a9200f4ef63c0013cab7ac (patch) | |
| tree | 68f1b7f77e1690d5e7021cb221f454dff146905f /htmx | |
| parent | de3aa62dbc608ec1fbcf676275340644ed1fd031 (diff) | |
| download | x-39660d978304ebce08a9200f4ef63c0013cab7ac.tar.xz x-39660d978304ebce08a9200f4ef63c0013cab7ac.zip | |
cmd/hdrwtch: closer to implementation
Signed-off-by: Xe Iaso <me@xeiaso.net>
Diffstat (limited to 'htmx')
| -rw-r--r-- | htmx/htmx.go | 7 | ||||
| -rw-r--r-- | htmx/htmx.templ | 2 | ||||
| -rw-r--r-- | htmx/htmx_templ.go | 59 |
3 files changed, 22 insertions, 46 deletions
diff --git a/htmx/htmx.go b/htmx/htmx.go index 8745c12..123259a 100644 --- a/htmx/htmx.go +++ b/htmx/htmx.go @@ -3,6 +3,8 @@ package htmx import ( "embed" "net/http" + + "within.website/x/internal" ) //go:generate go run github.com/a-h/templ/cmd/templ@latest generate @@ -23,7 +25,10 @@ const URL = "/.within.website/x/htmx/" // // If you use the Core or Use functions, you will need to ensure that HTMX is mounted at the correct path. Otherwise it will not be able to find the required JavaScript files. func Mount(mux *http.ServeMux) { - mux.Handle(URL, http.StripPrefix(URL, http.FileServer(http.FS(Static)))) + hdlr := http.StripPrefix(URL, http.FileServer(http.FS(Static))) + hdlr = internal.UnchangingCache(hdlr) + + mux.Handle(URL, hdlr) } // HTTP request headers diff --git a/htmx/htmx.templ b/htmx/htmx.templ index 41840d9..321bda5 100644 --- a/htmx/htmx.templ +++ b/htmx/htmx.templ @@ -11,7 +11,7 @@ package htmx // // This is mostly based on the extensions that I personally use. templ Use(exts ...string) { - @Core() + <script src={ URL + "htmx.js" }></script> for _, ext := range exts { <script src={ URL + ext + ".js" }></script> } diff --git a/htmx/htmx_templ.go b/htmx/htmx_templ.go index f9650e0..7505aa2 100644 --- a/htmx/htmx_templ.go +++ b/htmx/htmx_templ.go @@ -8,10 +8,17 @@ package htmx import "github.com/a-h/templ" import templruntime "github.com/a-h/templ/runtime" -// Core pulls in the HTMX core library. +// Use pulls in the HTMX core library and any extensions. +// +// Right now the extensions you can choose from are: // -// This is intended to be used inside a templ template and will probably not work outside of it. -func Core() templ.Component { +// - event-header +// - path-params +// - remove-me +// - websocket +// +// This is mostly based on the extensions that I personally use. +func Use(exts ...string) templ.Component { return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) @@ -36,7 +43,7 @@ func Core() templ.Component { var templ_7745c5c3_Var2 string templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(URL + "htmx.js") if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `htmx.templ`, Line: 7, Col: 30} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `htmx.templ`, Line: 14, Col: 30} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2)) if templ_7745c5c3_Err != nil { @@ -46,53 +53,17 @@ func Core() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - return templ_7745c5c3_Err - }) -} - -// Use pulls in the HTMX core library and any extensions. -// -// Right now the extensions you can choose from are: -// -// - event-header -// - path-params -// - remove-me -// - websocket -// -// This is mostly based on the extensions that I personally use. -func Use(exts ...string) templ.Component { - return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { - templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) - if !templ_7745c5c3_IsBuffer { - defer func() { - templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) - if templ_7745c5c3_Err == nil { - templ_7745c5c3_Err = templ_7745c5c3_BufErr - } - }() - } - ctx = templ.InitializeContext(ctx) - templ_7745c5c3_Var3 := templ.GetChildren(ctx) - if templ_7745c5c3_Var3 == nil { - templ_7745c5c3_Var3 = templ.NopComponent - } - ctx = templ.ClearChildren(ctx) - templ_7745c5c3_Err = Core().Render(ctx, templ_7745c5c3_Buffer) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } for _, ext := range exts { _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<script src=\"") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - var templ_7745c5c3_Var4 string - templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(URL + ext + ".js") + var templ_7745c5c3_Var3 string + templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(URL + ext + ".js") if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `htmx.templ`, Line: 23, Col: 33} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `htmx.templ`, Line: 16, Col: 33} } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4)) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } |
