Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/stephenlacy/daz
Composable HTML components in Golang
https://github.com/stephenlacy/daz
components golang html template
Last synced: 15 days ago
JSON representation
Composable HTML components in Golang
- Host: GitHub
- URL: https://github.com/stephenlacy/daz
- Owner: stephenlacy
- License: mit
- Created: 2020-12-08T16:05:00.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2023-04-19T14:07:53.000Z (over 1 year ago)
- Last Synced: 2024-05-21T13:05:04.396Z (6 months ago)
- Topics: components, golang, html, template
- Language: Go
- Homepage:
- Size: 667 KB
- Stars: 201
- Watchers: 5
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# daz
> Composable HTML components in Golang
[![GoDoc](https://godoc.org/github.com/stevelacy/daz?status.svg)](https://godoc.org/github.com/stevelacy/daz)![Go](https://github.com/stevelacy/daz/workflows/Go/badge.svg)
![daz carbon example](./carbon.png)
Daz is a "functional" alternative to using templates, and allows for nested components/lists
Also enables template-free server-side rendered components with support for nested lists. It is inspired by [HyperScript](https://github.com/hyperhype/hyperscript).A component can be created and used with simple functions:
```golang
// Example prop for a component
type User struct {
Name string
// ...
}func MyComponent(user User) HTML {
return H(
"div",
Attr{"class": "bg-grey-50"},
user.Name,
)
}func Root() HTML {
user := User{Name: "Daz"}
return H("html", MyComponent(user))
}// And used in a handler:
func Handler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(Root()()))
}
```Lists can be easily created without needing to embed a `range / end` in a template:
```golang
items := []HTML{
H("li", "item one"),
H("li", "item two"),
}element := H("ul", Attr{"class": "bg-grey-50"}, items)
div := H("div", element)
```### Install
```
import (
"github.com/stevelacy/daz"
)```
### Usage
#### func `H`
Create a HTML element:
```golang
H("div", ...attrs)```
#### struct `Attr`
HTML attributes:
```golang
Attr{
"class": "app",
"onClick": "javascriptFunc()",
}
```#### func `UnsafeContent`
This will bypass HTML sanitization and allow for direct injecting
```golanginjection := "alert('xss')"
root := H("div", UnsafeContent(injection))
// alert('xss')
```