Composable HTML components in Golang

Overview

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()",
}
You might also like...
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

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

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

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.

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

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

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

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

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

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",
    }
    
    opened by benkasminbullock 2
  • 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!

    opened by codemicro 1
  • 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

    opened by codemicro 1
Owner
Steve Lacy
Gopher drinking the Elixir. Building DevOps infrastructure for Counter Play Games
Steve Lacy
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

Asaf Shitrit 6 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

Qiniu Cloud 28 Aug 10, 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. The library allows you to call Go service methods from PHP with a minimal footprint, structures and []byte support.

Spiral Scout 1.1k Nov 23, 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

Antonio Ojea 4 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.

null 61 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

Walter Schulze 1k Nov 23, 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

null 51 Nov 3, 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

Shyamsunder Rathi 27 Jul 5, 2022
Meteoric golang nitro sniper, 0.1/0.5s claim time.

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

Azael 298 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.

Airbus DS GEO S.A. 84 Nov 23, 2022