Skip to content
Thanatat Tamtan edited this page Sep 23, 2023 · 7 revisions

Component can be used to render inside template, or render as standalone html (for htmx!).

Render inside template

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>
`)

Return standalone html

func tableBody(ctx *hime.Context) error {
    data := getData()
    return ctx.Component("table-body", data)
}

Return using Render

func tableBody(ctx *hime.Context) error {
    data := getData()
    return ctx.Render(`
        {{range .}}
            <tr>
                <td>{{.ID}}</td>
                <td>{{.Name}}</td>
            </tr>
        {{end}}
    `, data)
}

Clone this wiki locally