Composable HTML components in Golang

daz

Composable HTML components in Golang

GoDocGo

daz carbon example

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.

A component can be created and used with simple functions:

// Example prop for a component
type User struct {
	Name string
	// ...
}

func MyComponenet(user User) func() string {
	return H(
		"div",
		Attr{"class": "bg-grey-50"},
		user.Name,
	)
}

func Root() func() string {
	user := User{Name: "Daz"}
	html := H("html", MyComponenet(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:

items := []func() string{
	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:

H("div", ...attrs)

struct Attr

HTML attributes:

Attr{
	"class": "app",
	"onClick": "javascriptFunc()",
}
Owner
Steve Lacy
Gopher drinking the Elixir. Building DevOps infrastructure for Counter Play Games
Steve Lacy
Similar Resources

Minimal UART client in Golang that dumps LPC1343 chips that are locked at CRP1.

Howdy y'all, This is a quick and dirty client for the UART bootloader of the LPC1343, and probably other bootloaders in that chip family. This client

Dec 2, 2022

psutil for golang

gopsutil: psutil for golang This is a port of psutil (https://github.com/giampaolo/psutil). The challenge is porting all psutil functions on some arch

Dec 30, 2022

Cross platform locale detection for Golang

go-locale go-locale is a Golang lib for cross platform locale detection. OS Support Support all OS that Golang supported, except android: aix: IBM AIX

Aug 20, 2022

A Go (golang) library for parsing and verifying versions and version constraints.

go-version is a library for parsing versions and version constraints, and verifying versions against a set of constraints. go-version can sort a collection of versions properly, handles prerelease/beta versions, can increment versions, etc.

Jan 9, 2023

A port of the parser from graphql-js into golang

gqlparser This is a parser for graphql, written to mirror the graphql-js reference implementation as closely while remaining idiomatic and easy to use

Dec 27, 2022

Copier for golang, copy value from struct to struct and more

Copier I am a copier, I copy everything from one to another Features Copy from field to field with same name Copy from method to field with same name

Jan 8, 2023

A library for diffing golang structures

Diff A library for diffing golang structures and values. Utilizing field tags and reflection, it is able to compare two structures of the same type an

Dec 29, 2022

Use Golang to implement PHP's common built-in functions.

PHP2Go Use Golang to implement PHP's common built-in functions. About 140+ functions have been implemented. Install go get github.com/syyongx/php2go R

Dec 28, 2022

Extremely flexible golang deep comparison, extends the go testing package, tests HTTP APIs and provides tests suite

Extremely flexible golang deep comparison, extends the go testing package, tests HTTP APIs and provides tests suite

go-testdeep Extremely flexible golang deep comparison, extends the go testing package. Latest news Synopsis Description Installation Functions Availab

Jan 5, 2023
Comments
  • <img> doesn't need </img>

    doesn't need

    I tried your project:

    package main
    
    import (
    	"fmt"
    	"github.com/stevelacy/daz"
    )
    
    func main() {
    	img := daz.H("img", daz.Attr{"src": "https://www.sljfaq.org/images/birthdays.png"})
    	body := daz.H("body", img)
    	fmt.Printf("%s", daz.H("html", daz.H("head"), body)())
    }
    

    The output looks like this:

    <html><head></head><body><img src='https://www.sljfaq.org/images/birthdays.png'></img></body></html>
    

    You don't need </img> after the <img>.

    The W3 validator gives an error on adding this input, so I would assume it's an error to add this. I looked at the source code but didn't see a way to switch that off.

    Some other things which don't need an ending tag are

    var noCloseTags = []string{
            "area",
            "br",
            "hr",
            "image",
            "input",
            "img",
            "link",
            "meta",
    }
    
  • Fix README component example

    Fix README component example

    I noticed this one just as the other PR got merged 😅 This should be everything

    In this case, the example wouldn't run, and also saw some of the HTML being escaped because of the use of func() string instead of HTML

    This is a neat little library, by the way!

  • Make README.md consistent with changes in eaee308

    Make README.md consistent with changes in eaee308

    eaee308

    :) took me a moment to figure this out what was going on when I was seeing output looking like <div><ul class='bg-grey-50'>[0x49f6a0 0x49f6a0]</ul></div> haha

Simple HTML Modification in Go

Simple HTML Modification in Go Do you grin at the sight of html.Node ? Me too. Modifying HTML in Go should be simple. ???? Human friendly: query langu

Sep 29, 2021
hdq - HTML DOM Query Language for Go+

hdq - HTML DOM Query Language for Go+ Summary about hdq hdq is a Go+ package for processing HTML documents. Tutorials Collect links of a html page How

Dec 13, 2022
Goridge is high performance PHP-to-Golang codec library which works over native PHP sockets and Golang net/rpc package.
Goridge is high performance PHP-to-Golang codec library which works over native PHP sockets and Golang net/rpc package.

Goridge is high performance PHP-to-Golang codec library which works over native PHP sockets and Golang net/rpc package. The library allows you to call Go service methods from PHP with a minimal footprint, structures and []byte support.

Dec 28, 2022
memresolver is an in-memory golang resolver that allows to override current golang Lookup func literals

mem-resolver memresolver is an in-memory golang resolver that allows to override current golang Lookup func literals How to use it Create your custom

Jun 23, 2022
Govalid is a data validation library that can validate most data types supported by golang

Govalid is a data validation library that can validate most data types supported by golang. Custom validators can be used where the supplied ones are not enough.

Apr 22, 2022
Code Generation for Functional Programming, Concurrency and Generics in Golang

goderive goderive derives mundane golang functions that you do not want to maintain and keeps them up to date. It does this by parsing your go code fo

Dec 25, 2022
Golang source code parsing, usage like reflect package

gotype Golang source code parsing, usage like reflect package English 简体中文 Usage API Documentation Examples License Pouch is licensed under the MIT Li

Dec 9, 2022
Golang library to act on structure fields at runtime. Similar to Python getattr(), setattr(), hasattr() APIs.

go-attr Golang library to act on structure fields at runtime. Similar to Python getattr(), setattr(), hasattr() APIs. This package provides user frien

Dec 16, 2022
Meteoric golang nitro sniper, 0.1/0.5s claim time.
Meteoric golang nitro sniper, 0.1/0.5s claim time.

Meteoric golang nitro sniper, 0.1/0.5s claim time.

Apr 3, 2021
Golang bindings for GDAL

Golang bindings for GDAL Goals Godal aims at providing an idiomatic go wrapper around the GDAL library: Function calls return a result and an error.

Dec 16, 2022