ghompl

package module
v0.0.0-...-a4413f7 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2020 License: GPL-3.0 Imports: 2 Imported by: 0

README

ghompl

Go HTML templating in code using functions

What? Why?

I was using the html/template package for one of my projects but it got dirty really quickly once doing nested templates, so this is an attempt to make those problems disappear.

This implementation is very concise at ~100 SLoC, out of which half are there just for user convenience.

How?

Everything is Go code.

  • Import ghompl import . "github.com/jerwuqu/ghompl"
  • Create elements myDiv := E("div")
  • Attach attributes myDiv.Attrs["id"] = "my-div"
  • Append some text myDiv.Append(T("Hello!"))
  • Or do it all at once myDiv := EAT("div", A{"id": "my-div"}, "Hello!")

EAT???

  • E = Element
  • A = Attributes
  • T = Text

Skim through the implementation to see all possible combinations and/or the tests for examples of usage.

Tests?

go test github.com/jerwuqu/ghompl/test

License?

GNU GPL v3.0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type A

type A map[string]string

Shorthand type for Attributes

type Ghompl

type Ghompl struct {
	El    string
	Attrs A
	Body  []Ghompl
	HTML  string
}

Fundamental HTML building block

func E

func E(el string, body ...Ghompl) Ghompl

Create an element node

func EA

func EA(el string, attrs A, body ...Ghompl) Ghompl

Create an element node with attributes

func EAT

func EAT(el string, attrs A, str string) Ghompl

Create an element node with attributes and a text body

func ET

func ET(el string, str string) Ghompl

Create an element node with a text body

func Frag

func Frag() Ghompl

Create a fragment node

func HTML

func HTML(str string) Ghompl

Create a raw HTML node

func T

func T(str string) Ghompl

Create a text node

func (*Ghompl) Append

func (g *Ghompl) Append(node Ghompl)

Append a node

func (Ghompl) Render

func (g Ghompl) Render() string

Render the node to an HTML string Assumes element names and attribute keys are lowercase and contain no spaces

Jump to

Keyboard shortcuts

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