Comments (3)
Hi @attila-kun , could you help with the investigation of this by providing your templ version, and also the generated code that templ generate creates please?
from templ.
Hi @joerdav, my templ version is: v0.2.543
Also, I simplified the example a bit, as it's actually not specific to HTMX attributes. Consider this:
templ buttonTempl(id int) {
<button onclick={clickHandler(id, arguments[0])}>Click me!</button>
}
script clickHandler(id int, pointerEvent interface{}) {
console.log(id, event);
}
The generated code is:
// Code generated by templ - DO NOT EDIT.
// templ: version: v0.2.543
package main
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import "context"
import "io"
import "bytes"
func buttonTempl(id int) templ.Component {
return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) {
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer)
if !templ_7745c5c3_IsBuffer {
templ_7745c5c3_Buffer = templ.GetBuffer()
defer templ.ReleaseBuffer(templ_7745c5c3_Buffer)
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templ.RenderScriptItems(ctx, templ_7745c5c3_Buffer, clickHandler(id, arguments[0]))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<button onclick=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var2 templ.ComponentScript = clickHandler(id, arguments[0])
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var2.Call)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\">Click me!</button>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if !templ_7745c5c3_IsBuffer {
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W)
}
return templ_7745c5c3_Err
})
}
func clickHandler(id int, pointerEvent interface{}) templ.ComponentScript {
return templ.ComponentScript{
Name: `__templ_clickHandler_0cd5`,
Function: `function __templ_clickHandler_0cd5(id, pointerEvent){console.log(id, event);
}`,
Call: templ.SafeScript(`__templ_clickHandler_0cd5`, id, pointerEvent),
CallInline: templ.SafeScriptInline(`__templ_clickHandler_0cd5`, id, pointerEvent),
}
}
The error: ./button_templ.go:26:93: undefined: arguments
Which makes sense because arguments
is indeed not a Go-accessible variable. It only exists at runtime, in the browser, not at render time. That being said, I'm not sure what a nice resolution to this problem is. One workaround is to do this:
templ buttonTempl(id int) {
<button id={fmt.Sprintf("%d", id)} onclick="clickHandler(this, arguments[0])">Click me!</button>
<script>
function clickHandler(target, pointerEvent) {
console.log(target.id, event);
}
</script>
}
But this is not ideal because it loses the benefits of the script
block, e.g. being rendered only once.
from templ.
I would recommend the second solution you have provided. The syntax of onclick={clickHandler(id, arguments[0])}
only works with Go parameters as you mentioned, and the values are resolved server side.
An alternative would be for you to grab one of the parameters in your script.
templ buttonTempl(id int) {
<button onclick={clickHandler(id)}>Click me!</button>
}
script clickHandler(id int) {
console.log(id, arguments[0]);
}
I'll close this for now, and if you have any other problems I'd direct you to either the templ Gopher slack channel, or the discussions section of this repo, as this isn't a bug or feature request.
from templ.
Related Issues (20)
- Getting this error while using the latest version or templ and gopls HOT 11
- proposal: improve developer experience for basic passing of data to JS expressions HOT 8
- Templ config for passsing additional variables to component HOT 1
- Proxy not ready, retrying infinitely HOT 2
- Improved dev flow HOT 1
- Proposal: Introduce JsGenericVar for Event and Element Access in element event handlers HOT 5
- `templ generate` takes a long time for unformatted files HOT 6
- refactor: update `templ generate` to support pwd being a symlink HOT 3
- Templ formatting breaks imports at v0.2.747 HOT 7
- Composable templates do not render properly in tests HOT 1
- App freeze using air proxy HOT 1
- templ-vscode "Request textDocument/codeAction failed" HOT 2
- Streaming Not Working
- [Proposal] Improve embedding components HOT 2
- proposal: Go comments within HTML tags HOT 1
- Raw go errors if block ends with a comment
- LSP crashes after being started by kakoune-lsp HOT 4
- How to prevent cache HOT 1
- .templ file with only script elements leads to unused import HOT 1
- Anyway to instantiate a templ once per rendering context? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from templ.