https://github.com/mbertschler/blocks
Build components that render to HTML in Go, similar to React.js
https://github.com/mbertschler/blocks
go gui html react
Last synced: 5 months ago
JSON representation
Build components that render to HTML in Go, similar to React.js
- Host: GitHub
- URL: https://github.com/mbertschler/blocks
- Owner: mbertschler
- License: apache-2.0
- Created: 2017-05-03T20:35:28.000Z (about 9 years ago)
- Default Branch: main
- Last Pushed: 2023-06-12T21:43:22.000Z (about 3 years ago)
- Last Synced: 2024-06-20T19:25:30.041Z (about 2 years ago)
- Topics: go, gui, html, react
- Language: Go
- Homepage:
- Size: 134 KB
- Stars: 6
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Blocks rendering library
========================
 [](https://pkg.go.dev/github.com/mbertschler/blocks/html?tab=doc)
Blocks is a Go library for writing HTML similar to React components.
Example
-------
Applications can rendern their user interface like in this example [examples/html/html.go](./examples/html/html.go). You can run it by running `cd examples/html && go run html.go`. Full code of this demo:
```go
package main
import (
"fmt"
"github.com/mbertschler/blocks/html"
)
func main() {
root := html.Blocks{
// Option 1: directly add an element
html.Doctype("html"),
html.Html(nil,
// Option 2: struct that implements Block interface (RenderHTML() Block)
HeadBlock{html.Name("key").Content("super")},
// Option 3: function that returns a Block
BodyBlock("Hello, world! :)
"),
),
}
out, err := html.RenderString(root)
if err != nil {
fmt.Println("Error:", err)
}
fmt.Println(out)
out, err = html.RenderMinifiedString(root)
if err != nil {
fmt.Println("Error:", err)
}
fmt.Println(out)
}
type HeadBlock struct {
html.Attributes
}
func (h HeadBlock) RenderHTML() html.Block {
return html.Head(nil,
html.Meta(h.Attributes),
)
}
func BodyBlock(in string) html.Block {
return html.Body(nil,
html.Main(html.Class("main-class\" href=\"/evil/link"),
html.H1(nil,
html.Text(in),
html.Br(),
html.UnsafeString(in),
),
),
)
}
```
License
-------
Blocks is released under the Apache 2.0 license. See [LICENSE](LICENSE).