No-frills html elements in pure golang.
import "github.com/accentdesign/gtml"
field := gtml.Div(
gtml.NA,
gtml.Label(gtml.Attrs{"for": "email"}, gtml.Text("Email")),
gtml.Input(gtml.Attrs{
"class": "form-input",
"id": "email",
"placeholder": "email@example.com",
"required": true,
"type": "email",
}),
gtml.P(gtml.Attrs{"class": "help-text"}, gtml.Text("Your email address.")),
)
field.Render(context.Background(), os.Stdout)renders:
<div>
<label for="email">Email</label>
<input class="form-input" id="email" placeholder="email@example.com" required type="email">
<p class="help-text">Your email address.</p>
</div>gtml is designed to work with templ. It implements the templ.Component interface.
Easily include them in a templ.Component.
templ Form() {
<form>
@field
</form>
}Or include a templ.Component in a gtml.Element as a child.
func childTemplate(text string) templ.Component {
return templ.ComponentFunc(func(ctx context.Context, w io.Writer) error {
_, err := io.WriteString(w, "<p>"+text+"</p>")
return err
})
}
gtml.Div(gtml.Attrs{"class": "parent"}, childTemplate("Hello, World!"))gtml also comes with a CLI tool to generate go from an html file.
install:
go install github.com/accentdesign/gtml/cmd/gtml@latestusage
gtml -helpgenerate go from html to file:
gtml generate < input.html > output.gogenerate go from html to stdout:
gtml generate < input.html