htmlgo
Type safe and modularize way to generate html on server side.
Download the package with go get -v github.com/theplant/htmlgo
and import the package with .
gives you simpler code:
import (
. "github.com/theplant/htmlgo"
)
also checkout full API documentation at: https://godoc.org/github.com/theplant/htmlgo
Create a simple div, Text will be escaped by html
banner := "We write html in Go"
comp := Div(
Text("123<h1>"),
Textf("Hello, %s", banner),
Br(),
)
Fprint(os.Stdout, comp, context.TODO())
Create a full html page
comp := HTML(
Head(
Meta().Charset("utf8"),
Title("My test page"),
),
Body(
Img("images/firefox-icon.png").Alt("My test image"),
),
)
Fprint(os.Stdout, comp, context.TODO())
Use RawHTML and Component
userProfile := func(username string, avatarURL string) HTMLComponent {
return ComponentFunc(func(ctx context.Context) (r []byte, err error) {
return Div(
H1(username).Class("profileName"),
Img(avatarURL).Class("profileImage"),
RawHTML("<svg>complicated svg</svg>\n"),
).Class("userProfile").MarshalHTML(ctx)
})
}
comp := Ul(
Li(
userProfile("felix<h1>", "http://image.com/img1.png"),
),
Li(
userProfile("john", "http://image.com/img2.png"),
),
)
Fprint(os.Stdout, comp, context.TODO())
More complicated customized component
comp := MySelect().Options([][]string{
{"1", "label 1"},
{"2", "label 2"},
{"3", "label 3"},
}).Selected("2")
Fprint(os.Stdout, comp, context.TODO())
Write a little bit of JavaScript and stylesheet
comp := Div(
Button("Hello").Id("hello"),
Style(`
.container {
background-color: red;
}
`),
Script(`
var b = document.getElementById("hello")
b.onclick = function(e){
alert("Hello");
}
`),
).Class("container")
Fprint(os.Stdout, comp, context.TODO())
An example about how to integrate into http.Handler, and how to do layout, and how to use context.
type User struct {
Name string
}
userStatus := func() HTMLComponent {
return ComponentFunc(func(ctx context.Context) (r []byte, err error) {
if currentUser, ok := ctx.Value("currentUser").(*User); ok {
return Div(
Text(currentUser.Name),
).Class("username").MarshalHTML(ctx)
}
return Div(Text("Login")).Class("login").MarshalHTML(ctx)
})
}
myHeader := func() HTMLComponent {
return Div(
Text("header"),
userStatus(),
).Class("header")
}
myFooter := func() HTMLComponent {
return Div(Text("footer")).Class("footer")
}
layout := func(in HTMLComponent) (out HTMLComponent) {
out = HTML(
Head(
Meta().Charset("utf8"),
),
Body(
myHeader(),
in,
myFooter(),
),
)
return
}
getLoginUserFromCookie := func(r *http.Request) *User {
return &User{Name: "felix"}
}
homeHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user := getLoginUserFromCookie(r)
ctx := context.WithValue(context.TODO(), "currentUser", user)
root := Div(
Text("This is my home page"),
)
Fprint(w, layout(root), ctx)
})
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/", nil)
homeHandler.ServeHTTP(w, r)
fmt.Println(w.Body.String())
An example show how to set different type of attributes
type MoreData struct {
Name string
Count int
}
comp := Div(
Input("username").
Type("checkbox").
Attr("checked", true).
Attr("more-data", &MoreData{Name: "felix", Count: 100}).
Attr("max-length", 10),
Input("username2").
Type("checkbox").
Attr("checked", false),
)
Fprint(os.Stdout, comp, context.TODO())
An example show how to set styles
comp := Div().
StyleIf("background-color:red; border:1px solid red;", true).
StyleIf("color:blue", true)
Fprint(os.Stdout, comp, context.TODO())
An example to use If, Iff
is for body to passed in as an func for the body depends on if condition not to be nil, If
is for directly passed in HTMLComponent
type Person struct {
Age int
}
var p *Person
name := "Leon"
comp := Div(
Iff(p != nil && p.Age > 18, func() HTMLComponent {
return Div().Text(name + ": Age > 18")
}).ElseIf(p == nil, func() HTMLComponent {
return Div().Text("No person named " + name)
}).Else(func() HTMLComponent {
return Div().Text(name + ":Age <= 18")
}),
)
Fprint(os.Stdout, comp, context.TODO())