gotml

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2024 License: MIT Imports: 3 Imported by: 0

README

gotml

gotml is a component-based HTML templating library for Go. It allows you to create and render HTML structures using a component-oriented approach, offering an alternative to Go's built-in template package. With gotml, you can define reusable components and compose HTML elements in a more modular and expressive way.

Overview

Why Use gotml?
  • Component-Based Approach: Unlike Go templates, which are primarily text-based and less modular, gotml enables you to define components as functions. This allows for better reuse and organization of HTML structures.

  • Reduce Nesting: In Go templates, refactors can easily create merge conflicts due to deep nesting of HTML. In contrast, gotml

  • Dynamic Composition: Easily compose complex HTML structures by combining simple components. gotml supports variadic parameters for components, making it straightforward to include multiple children or attributes.

  • Improved Readability: gotml’s approach helps to maintain readability by separating concerns into manageable components, avoiding the verbosity and complexity often found in traditional template-based HTML generation.

Basic Usage: Define a tree structure

You can define a simple tree structure just like HTML or other frameworks.

package main

import (
	"fmt"

	g "github.com/philip-peterson/gotml"
)

func main() {
	ctx := g.Bag{}

	var App g.Component = func(attrs *g.AttrList, children ...g.GotmlTree) g.GotmlTree {
		return g.Tree("html").Children(
			g.Tree("head"),
			g.Tree("body").Children(
				g.Tree("div").
					Attr("style", "color: red").
					Children("Lorem ipsum"),
				g.Tree("hr"),
				g.Tree("div").Children("Hello world"),
			),
		)
	}

	myTree := g.Tree(App)
	result := g.Render(ctx, myTree)

	fmt.Println(result)
}

The output*:

<html>
   <head />
   <body>
      <div style="color: red">
         Lorem ipsum
      </div>
      <hr />
      <div>
         Hello world
      </div>
   </body>
</html>
Advanced Usage: Passing through Children

Define a component with attributes and children:

package main

import (
	"fmt"

	g "github.com/philip-peterson/gotml"
)

func main() {
	var BorderedDiv g.Component = func(attrs *g.AttrList, children ...g.GotmlTree) g.GotmlTree {
		return g.Tree("div").
			Attr("style", "border: 3px double black").
			Children(
				g.AsAny(children)...,
			)
	}
}

Note that due to Go's quirk where it cannot convert a slice of a type, []T to a slice of any, []any without help, we use g.AsAny(...) to perform this conversion.

Now, we can render the component to HTML:

myTree := g.Tree(BorderedDiv).Children(
    g.Tree("p").Children("Hello, world!"),
)

result := g.Render(ctx, myTree)
fmt.Println(result)

Creating the output*:

<div style="border: 3px double black">
   <p>
      Hello, world!
   </p>
</div>

The full program is therefore as follows:

package main

import (
	"fmt"

	g "github.com/philip-peterson/gotml"
)

func main() {
	var BorderedDiv g.Component = func(attrs *g.AttrList, children ...g.GotmlTree) g.GotmlTree {
		return g.Tree("div").
			Attr("style", "border: 3px double black").
			Children(
				g.AsAny(children)...,
			)
	}

	ctx := map[string]interface{}{}

	myTree := g.Tree(BorderedDiv).Children(
		g.Tree("p").Children("Hello, world!"),
	)

	result := g.Render(ctx, myTree)
	fmt.Println(result)
}

For more detailed usage and examples, please see the tests.

* Output has been prettified for readability.

Documentation

Overview

You can edit this code! Click here and start typing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AsAny added in v0.9.5

func AsAny(children []GotmlTree) []any

func Render added in v0.9.2

func Render(ctx Bag, tree GotmlTree) string

Types

type AttrList added in v0.9.4

type AttrList struct {
	This AttrNode
	Next *AttrList
}

func (*AttrList) ToBag added in v0.9.4

func (list *AttrList) ToBag() Bag

type AttrNode added in v0.9.4

type AttrNode struct {
	Key   string
	Value interface{}
}

func Attr

func Attr(k string, v interface{}) AttrNode

type Bag

type Bag = map[string]interface{}

type Component

type Component func(*AttrList, ...GotmlTree) GotmlTree

type GotmlTree

type GotmlTree struct {
	// contains filtered or unexported fields
}

func Tree added in v1.0.0

func Tree(tagName any) GotmlTree

func (GotmlTree) Attr added in v0.9.4

func (g GotmlTree) Attr(key string, value interface{}) GotmlTree

func (GotmlTree) Children added in v0.9.4

func (g GotmlTree) Children(children ...any) GotmlTree

Jump to

Keyboard shortcuts

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