Skip to content
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.

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

Render to a string

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

Clone this wiki locally