daz

package module
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 13, 2021 License: MIT Imports: 3 Imported by: 3

README

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()",
}
func UnsafeContent

This will bypass HTML sanitization and allow for direct injecting


injection := "<script>alert('xss')</script>"
root := H("div", UnsafeContent(injection))
// <script>alert('xss')</script>

Documentation

Overview

Package daz is a library for building composable HTML components in Go. It is a functional alternative to using templates, and allows for nested components/lists.

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 https://github.com/hyperhype/hyperscript

Basic usage:

element := H("div", Attr{"class": "bg-grey-50"})

html := H("html", element)

w.Write([]byte(html()))

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"})

div := H("div", element)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func UnsafeContent added in v0.1.3

func UnsafeContent(str string) dangerousContents

UnsafeContent allows injection of JS or HTML from functions

Types

type Attr

type Attr map[string]string

Attr is a HTML element attribute <a href="#"> => Attr{"href": "#"}

type HTML added in v0.1.3

type HTML func() string

func H

func H(el string, attrs ...interface{}) HTML

H is the base HTML func

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL