gohtml

package module
v0.0.0-...-1231c86 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2021 License: MIT Imports: 5 Imported by: 0

README

GOHTML

An encoder for a Go struct to HTML

Using the "reflect" package and recursion this package is able to convert a complex go struct into HTML

Features
  • Struct
  • Embedded struct
  • Slice
  • Table encode option
  • Omit Empty
  • Map (Needs implementation and testing)
  • Custom ID (Unsure on whether this is needed)
Examples
Simple Encode
package main

import (
    "fmt"
    "github.com/DrGrimshaw/gohtml"
)

type Example struct {
	Name string `html:"l=Name,e=span,c=name"`
}

func main() {
    fmt.Print(gohtml.Encode(Example{Name: "John Doe"}))
}
Output
<div><span>Name</span><span class='name'>John</span></div>
Table Format
package main

import (
    "fmt"
    "github.com/DrGrimshaw/gohtml"
)

type Example struct {
	Name string `html:"l=Name,e=span,c=name"`
	Age string `html:"l=Age,e=span,c=age"`
	Location string `html:"l=Location,e=span,c=location"`
}

type ExampleArr struct {
    Examples []Example `html:"row"`
}

func main() {
    tableExample := ExampleArr{Examples: []Example{{Name: "John Doe",Age: "30",Location: "Washington DC"}}}

    fmt.Print(gohtml.Encode(tableExample))
}
Output
<table>
    <thead>
        <tr>
            <td>Name</td>
            <td>Age</td>
            <td>Location</td>
        </tr>
    </thead>
    <tr>
        <td><span class='name'>John Doe</span></td>
        <td><span class='age'>30</span></td>
        <td><span class='location'>Washington DC</span></td>
    </tr>
</table>

Gopher mascot

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Encode

func Encode(structure interface{}) (string, error)

Encode finds and extracts data for HTML output The only error that returns is due to a malformed struct tags

Types

This section is empty.

Jump to

Keyboard shortcuts

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