-
Notifications
You must be signed in to change notification settings - Fork 2
Component
Thanatat Tamtan edited this page Jun 9, 2026
·
9 revisions
Component can be used to render inside template, or render as standalone html (for htmx!).
Component names must be unique. Registering the same name twice (via ParseComponent, ParseComponentFile, or Component) panics.
t := app.Template()
t.ParseComponent("table-body", `
{{range .}}
<tr>
<td>{{.ID}}</td>
<td>{{.Name}}</td>
</tr>
{{end}}
`)
t.Parse("index", `
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
</tr>
</thead>
<tbody>
{{component "table-body" .List}}
</tbody>
</table>
`)func tableBody(ctx *hime.Context) error {
data := getData()
return ctx.Component("table-body", data)
}func tableBody(ctx *hime.Context) error {
data := getData()
return ctx.Render(`
{{range .}}
<tr>
<td>{{.ID}}</td>
<td>{{.Name}}</td>
</tr>
{{end}}
`, data)
}ctx.RenderComponentToString renders a component to a string instead of writing
it to the response. It is handy for composing a response from several components
(for example, htmx out-of-band swaps — see HTMX).
func update(ctx *hime.Context) error {
row, err := ctx.RenderComponentToString("row", item)
if err != nil {
return err
}
return ctx.HTML(row)
}