How to pass data to html such as alpinejs? #519
-
I have a usecase that I want to pass an record id to alpinejs
Can we do this? I have not found a way to do that, especially if we want to pass a js object to script too... |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 4 replies
-
components.templpackage main
templ Page(data any) {
<div x-data={ toJSON(data) }>
Content
</div>
} main.gopackage main
import (
"context"
"encoding/json"
"os"
)
func toJSON(v any) string {
b, _ := json.Marshal(v)
return string(b)
}
func main() {
data := map[string]any{
"i_want_to_put_here": "<data>",
}
Page(data).Render(context.Background(), os.Stdout)
}
|
Beta Was this translation helpful? Give feedback.
-
@a-h Is it possible if we can do something like this?
|
Beta Was this translation helpful? Give feedback.
-
I've ended up just doing this. templ Page(str string) {
<div x-data={ "key: '" + str + "'" }>...</div>
} However is probably nicer and safer to use the method solution you suggested. |
Beta Was this translation helpful? Give feedback.
-
Strangely when alpinejs is used like below: templ whitelistRow(item query.PsaVwWhitelistDetail) {
<tr x-data="{ editMode: false }">
<td class="py-4 px-6 text-left">
<template x-if="!editMode">
<span x-text={ item.UserEmail }></span>
</template>
<template x-if="editMode">
<input type="text" name="UserEmail" x-model={ item.UserEmail } class="editable-input"/>
</template>
</td>
<td class="py-4 px-6 text-left">
<template x-if="!editMode">
<span x-text={ item.WhitelistReason }></span>
</template>
<template x-if="editMode">
<input type="text" name="WhitelistReason" x-model={ item.WhitelistReason } class="editable-input"/>
</template>
</td>
<td class="py-4 px-6 text-left">
<template x-if="!editMode">
<span x-text={ item.CategoryName }></span>
</template>
<template x-if="editMode">
<input type="text" name="CategoryName" x-model={ item.CategoryName } class="editable-input"/>
</template>
</td>
<td class="py-4 px-6 text-left">
<template x-if="!editMode">
<span x-text={ item.EventName }></span>
</template>
<template x-if="editMode">
<input type="text" name="EventName" x-model={ item.EventName } class="editable-input"/>
</template>
</td>
<td class="py-4 px-6 text-left">
<template x-if="!editMode">
<span x-text={ strconv.FormatInt(item.UserID, 10) }></span>
</template>
<template x-if="editMode">
<input type="number" name="UserID" x-model={ strconv.FormatInt(item.UserID,10) } class="editable-input"/>
</template>
</td>
<td class="py-4 px-6 text-left">
<template x-if="!editMode">
<span x-text={ strconv.FormatInt(item.EventID.Int64, 10) }></span>
</template>
<template x-if="editMode">
<input type="number" name="EventID" x-model={ strconv.FormatInt(item.EventID.Int64, 10) } class="editable-input"/>
</template>
</td>
<td class="py-4 px-6 text-left">
<button x-show="!editMode" @click="editMode = true" class="k-button k-button-solid-primary px-4 py-2 font-semibold">
Update
</button>
<button x-show="editMode" @click="editMode = false" class="k-button k-button-solid-primary px-4 py-2 font-semibold">
Save
</button>
</td>
</tr>
} Only the INT fields display. The String fields are not rendered. See the output below. <tr x-data="{ editMode: false }">
<td class="py-4 px-6 text-left">
<template x-if="!editMode"
><span x-text="[email protected]"></span></template
><span x-text="[email protected]"></span
><template x-if="editMode"
><input
type="text"
name="UserEmail"
x-model="[email protected]"
class="editable-input"
/></template>
</td>
<td class="py-4 px-6 text-left">
<template x-if="!editMode"><span x-text="test"></span></template
><span x-text="test"></span
><template x-if="editMode"
><input
type="text"
name="WhitelistReason"
x-model="test"
class="editable-input"
/></template>
</td>
<td class="py-4 px-6 text-left">
<template x-if="!editMode"><span x-text="Test"></span></template
><span x-text="Test"></span
><template x-if="editMode"
><input
type="text"
name="CategoryName"
x-model="Test"
class="editable-input"
/></template>
</td>
<td class="py-4 px-6 text-left">
<template x-if="!editMode"><span x-text="kongres"></span></template
><span x-text="kongres"></span
><template x-if="editMode"
><input
type="text"
name="EventName"
x-model="kongres"
class="editable-input"
/></template>
</td>
<td class="py-4 px-6 text-left">
<template x-if="!editMode"><span x-text="241"></span></template
><span x-text="241">241</span
><template x-if="editMode"
><input type="number" name="UserID" x-model="241" class="editable-input"
/></template>
</td>
<td class="py-4 px-6 text-left">
<template x-if="!editMode"><span x-text="1"></span></template
><span x-text="1">1</span
><template x-if="editMode"
><input type="number" name="EventID" x-model="1" class="editable-input"
/></template>
</td>
<td class="py-4 px-6 text-left">
<button
x-show="!editMode"
@click="editMode = true"
class="k-button k-button-solid-primary px-4 py-2 font-semibold"
>
Update
</button>
<button
x-show="editMode"
@click="editMode = false"
class="k-button k-button-solid-primary px-4 py-2 font-semibold"
style="display: none"
>
Save
</button>
</td>
</tr> |
Beta Was this translation helpful? Give feedback.
components.templ
main.go
go run .
The content is attribute escaped, but is valid JSON.
Passing JavaScript objects on the client side.
I'm not familiar with alpine.js, but what you want seems similar to https://htmx.org/attributes/hx-vals/ in HTMX which can co…