htmlgo

package module
v0.0.0-...-2e9f4b9 Latest Latest
Warning

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

Go to latest
Published: May 5, 2020 License: MIT Imports: 7 Imported by: 4

README

htmlgo

A library for writing type-safe HTML in Go

This blog post provides a short introduction.

Why?

As much as I like the simplicity and the contextual escaping of html/template, it doesn't provide much type safety. Pipelines (e.g. {{.DoesNotExist.Value}}) can produce errors at runtime, which should be caught during compilation. Using nested templates with html/template can become hard to maintain, as a template user has to inspect the pipelines within a templates to infer the data format to be passed into the template. As a bonus, using Go functions to produce HTML elements prevents malformed HTML.

This library is inspired by Haskell's blaze-html.

Status

  • Support for all HTML5 tags and attributes
  • Secure, contextual escaping (based on html/template)
  • Not optimised for performance, expect it to be significantly slower than html/template

API

Tags

Functions for all HTML tags are part of the top-level package htmlgo. The function signatures are Tagname(attrs []attributes.Attribute, children ...HTML) HTML To omit the first argument, i.e. to create an element without attributes, there are functions with an underscore suffix Tagname_(children ...HTML) HTML to reduce verbosity.

Attributes

Functions to create attributes are located in the package htmlgo/attributes. Use Attr(attrs ...attributes.Attribute) from htmlgo as a less verbose way to create a slice of attributes. The function signatures are Attributename(data interface{}, templates ...string) Attribute. The data will be placed into the given templates using html/template and, therefore, follows the same syntax. Note, as templates is a variadic argument, it can be omitted entirely, in which case a {{.}} template is used. Data provided as data will be escaped, whereas the templates itself can be used to provide values which shall not be escaped. Again, there are convenience functions with an underscore suffix to omit the first argument, which is data in this case Attributename_(templates ...string) Attribute. Use the underscore-suffixed attribute functions wisely, as they will not escape their arguments. A good rule of thumb is that you should never pass a variable into the suffixed functions, only string literals.

The dataset attributes data-* can be added using Dataset(key, value string).

Example

package main

import (
    "fmt"

    . "github.com/julvo/htmlgo"
    a "github.com/julvo/htmlgo/attributes"
)

func main() {
    var numberDivs HTML
    for i := 0; i < 3; i++ {
        numberDivs += Div(Attr(a.Style_("font-family:monospace;")),
                          Text(i))
    }

    page :=
        Html5_(
            Head_(),
            Body_(
                H1_(Text("Welcome <script>")),
                numberDivs,
                Div(Attr(a.Dataset("hello", "htmlgo"))),
                Script_(JavaScript("alert('This is escaped');")),
                Script_(JavaScript("This is escaped", "alert({{.}});"))))

    fmt.Println(page)
}

will output:

<!DOCTYPE HTML>
<html>
  <head>
  </head>
  <body>
    <h1>
      Welcome &lt;script&gt;
    </h1>
    <div style="font-family:monospace;">
      0
    </div>
    <div style="font-family:monospace;">
      1
    </div>
    <div style="font-family:monospace;">
      2
    </div>
    <div data-hello="htmlgo">
    </div>
    <script>
      "alert('This is escaped');"
    </script>
    <script>
      alert("This is escaped");
    </script>
  </body>
</html>

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Attr

func Attr(attrs ...a.Attribute) []a.Attribute

Build a slice of type []Attribute for cosmetic purposes

func WriteTo

func WriteTo(w io.Writer, h HTML)

Types

type HTML

type HTML string
const DoctypeHtml5 HTML = "<!DOCTYPE HTML>"

func A

func A(attrs []a.Attribute, children ...HTML) HTML

func A_

func A_(children ...HTML) HTML

func Abbr

func Abbr(attrs []a.Attribute, children ...HTML) HTML

func Abbr_

func Abbr_(children ...HTML) HTML

func Acronym

func Acronym(attrs []a.Attribute, children ...HTML) HTML

func Acronym_

func Acronym_(children ...HTML) HTML

func Address

func Address(attrs []a.Attribute, children ...HTML) HTML

func Address_

func Address_(children ...HTML) HTML

func Applet

func Applet(attrs []a.Attribute, children ...HTML) HTML

func Applet_

func Applet_(children ...HTML) HTML

func Area

func Area(attrs []a.Attribute) HTML

func Area_

func Area_() HTML

func Article

func Article(attrs []a.Attribute, children ...HTML) HTML

func Article_

func Article_(children ...HTML) HTML

func Aside

func Aside(attrs []a.Attribute, children ...HTML) HTML

func Aside_

func Aside_(children ...HTML) HTML

func Audio

func Audio(attrs []a.Attribute, children ...HTML) HTML

func Audio_

func Audio_(children ...HTML) HTML

func B

func B(attrs []a.Attribute, children ...HTML) HTML

func B_

func B_(children ...HTML) HTML

func Base

func Base(attrs []a.Attribute) HTML

func Base_

func Base_() HTML

func Basefont

func Basefont(attrs []a.Attribute, children ...HTML) HTML

func Basefont_

func Basefont_(children ...HTML) HTML

func Bdi

func Bdi(attrs []a.Attribute, children ...HTML) HTML

func Bdi_

func Bdi_(children ...HTML) HTML

func Bdo

func Bdo(attrs []a.Attribute, children ...HTML) HTML

func Bdo_

func Bdo_(children ...HTML) HTML

func Bgsound

func Bgsound(attrs []a.Attribute, children ...HTML) HTML

func Bgsound_

func Bgsound_(children ...HTML) HTML

func Big

func Big(attrs []a.Attribute, children ...HTML) HTML

func Big_

func Big_(children ...HTML) HTML
func Blink(attrs []a.Attribute, children ...HTML) HTML
func Blink_(children ...HTML) HTML

func Blockquote

func Blockquote(attrs []a.Attribute, children ...HTML) HTML

func Blockquote_

func Blockquote_(children ...HTML) HTML

func Body

func Body(attrs []a.Attribute, children ...HTML) HTML

func Body_

func Body_(children ...HTML) HTML

func Br

func Br(attrs []a.Attribute) HTML

func Br_

func Br_() HTML

func Button

func Button(attrs []a.Attribute, children ...HTML) HTML

func Button_

func Button_(children ...HTML) HTML

func Canvas

func Canvas(attrs []a.Attribute, children ...HTML) HTML

func Canvas_

func Canvas_(children ...HTML) HTML

func Caption

func Caption(attrs []a.Attribute, children ...HTML) HTML

func Caption_

func Caption_(children ...HTML) HTML

func Center

func Center(attrs []a.Attribute, children ...HTML) HTML

func Center_

func Center_(children ...HTML) HTML

func Cite

func Cite(attrs []a.Attribute, children ...HTML) HTML

func Cite_

func Cite_(children ...HTML) HTML

func Code

func Code(attrs []a.Attribute, children ...HTML) HTML

func Code_

func Code_(children ...HTML) HTML

func Col

func Col(attrs []a.Attribute) HTML

func Col_

func Col_() HTML

func Colgroup

func Colgroup(attrs []a.Attribute, children ...HTML) HTML

func Colgroup_

func Colgroup_(children ...HTML) HTML

func Datalist

func Datalist(attrs []a.Attribute, children ...HTML) HTML

func Datalist_

func Datalist_(children ...HTML) HTML

func Dd

func Dd(attrs []a.Attribute, children ...HTML) HTML

func Dd_

func Dd_(children ...HTML) HTML

func Del

func Del(attrs []a.Attribute, children ...HTML) HTML

func Del_

func Del_(children ...HTML) HTML

func Details

func Details(attrs []a.Attribute, children ...HTML) HTML

func Details_

func Details_(children ...HTML) HTML

func Dfn

func Dfn(attrs []a.Attribute, children ...HTML) HTML

func Dfn_

func Dfn_(children ...HTML) HTML

func Dir

func Dir(attrs []a.Attribute, children ...HTML) HTML

func Dir_

func Dir_(children ...HTML) HTML

func Div

func Div(attrs []a.Attribute, children ...HTML) HTML

func Div_

func Div_(children ...HTML) HTML

func Dl

func Dl(attrs []a.Attribute, children ...HTML) HTML

func Dl_

func Dl_(children ...HTML) HTML

func Doctype

func Doctype(t string) HTML

func Dt

func Dt(attrs []a.Attribute, children ...HTML) HTML

func Dt_

func Dt_(children ...HTML) HTML

func Element

func Element(tag string, attrs []a.Attribute, children ...HTML) HTML

func Em

func Em(attrs []a.Attribute, children ...HTML) HTML

func Em_

func Em_(children ...HTML) HTML

func Embed

func Embed(attrs []a.Attribute) HTML

func Embed_

func Embed_() HTML

func Fieldset

func Fieldset(attrs []a.Attribute, children ...HTML) HTML

func Fieldset_

func Fieldset_(children ...HTML) HTML

func Figcaption

func Figcaption(attrs []a.Attribute, children ...HTML) HTML

func Figcaption_

func Figcaption_(children ...HTML) HTML

func Figure

func Figure(attrs []a.Attribute, children ...HTML) HTML

func Figure_

func Figure_(children ...HTML) HTML

func Font

func Font(attrs []a.Attribute, children ...HTML) HTML

func Font_

func Font_(children ...HTML) HTML
func Footer(attrs []a.Attribute, children ...HTML) HTML
func Footer_(children ...HTML) HTML

func Form

func Form(attrs []a.Attribute, children ...HTML) HTML

func Form_

func Form_(children ...HTML) HTML

func Frame

func Frame(attrs []a.Attribute, children ...HTML) HTML

func Frame_

func Frame_(children ...HTML) HTML

func Frameset

func Frameset(attrs []a.Attribute, children ...HTML) HTML

func Frameset_

func Frameset_(children ...HTML) HTML

func H1

func H1(attrs []a.Attribute, children ...HTML) HTML

func H1_

func H1_(children ...HTML) HTML

func H2

func H2(attrs []a.Attribute, children ...HTML) HTML

func H2_

func H2_(children ...HTML) HTML

func H3

func H3(attrs []a.Attribute, children ...HTML) HTML

func H3_

func H3_(children ...HTML) HTML

func H4

func H4(attrs []a.Attribute, children ...HTML) HTML

func H4_

func H4_(children ...HTML) HTML

func H5

func H5(attrs []a.Attribute, children ...HTML) HTML

func H5_

func H5_(children ...HTML) HTML

func H6

func H6(attrs []a.Attribute, children ...HTML) HTML

func H6_

func H6_(children ...HTML) HTML
func Head(attrs []a.Attribute, children ...HTML) HTML

func Head_

func Head_(children ...HTML) HTML
func Header(attrs []a.Attribute, children ...HTML) HTML

func Header_

func Header_(children ...HTML) HTML

func Hgroup

func Hgroup(attrs []a.Attribute, children ...HTML) HTML

func Hgroup_

func Hgroup_(children ...HTML) HTML

func Hr

func Hr(attrs []a.Attribute) HTML

func Hr_

func Hr_() HTML

func Html

func Html(attrs []a.Attribute, children ...HTML) HTML

func Html5

func Html5(attrs []a.Attribute, children ...HTML) HTML

func Html5_

func Html5_(children ...HTML) HTML

func Html_

func Html_(children ...HTML) HTML

func I

func I(attrs []a.Attribute, children ...HTML) HTML

func I_

func I_(children ...HTML) HTML

func Iframe

func Iframe(attrs []a.Attribute, children ...HTML) HTML

func Iframe_

func Iframe_(children ...HTML) HTML

func Img

func Img(attrs []a.Attribute) HTML

func Img_

func Img_() HTML

func Input

func Input(attrs []a.Attribute) HTML

func Input_

func Input_() HTML

func Ins

func Ins(attrs []a.Attribute, children ...HTML) HTML

func Ins_

func Ins_(children ...HTML) HTML

func Isindex

func Isindex(attrs []a.Attribute, children ...HTML) HTML

func Isindex_

func Isindex_(children ...HTML) HTML

func Kbd

func Kbd(attrs []a.Attribute, children ...HTML) HTML

func Kbd_

func Kbd_(children ...HTML) HTML

func Keygen

func Keygen(attrs []a.Attribute, children ...HTML) HTML

func Keygen_

func Keygen_(children ...HTML) HTML

func Label

func Label(attrs []a.Attribute, children ...HTML) HTML

func Label_

func Label_(children ...HTML) HTML

func Legend

func Legend(attrs []a.Attribute, children ...HTML) HTML

func Legend_

func Legend_(children ...HTML) HTML

func Li

func Li(attrs []a.Attribute, children ...HTML) HTML

func Li_

func Li_(children ...HTML) HTML
func Link(attrs []a.Attribute) HTML
func Link_() HTML

func Listing

func Listing(attrs []a.Attribute, children ...HTML) HTML

func Listing_

func Listing_(children ...HTML) HTML

func Main

func Main(attrs []a.Attribute, children ...HTML) HTML

func Main_

func Main_(children ...HTML) HTML

func Map

func Map(attrs []a.Attribute, children ...HTML) HTML

func Map_

func Map_(children ...HTML) HTML

func Mark

func Mark(attrs []a.Attribute, children ...HTML) HTML

func Mark_

func Mark_(children ...HTML) HTML

func Marquee

func Marquee(attrs []a.Attribute, children ...HTML) HTML

func Marquee_

func Marquee_(children ...HTML) HTML
func Menu(attrs []a.Attribute, children ...HTML) HTML
func Menu_(children ...HTML) HTML

func Meta

func Meta(attrs []a.Attribute) HTML

func Meta_

func Meta_() HTML

func Meter

func Meter(attrs []a.Attribute, children ...HTML) HTML

func Meter_

func Meter_(children ...HTML) HTML
func Nav(attrs []a.Attribute, children ...HTML) HTML
func Nav_(children ...HTML) HTML

func Nobr

func Nobr(attrs []a.Attribute, children ...HTML) HTML

func Nobr_

func Nobr_(children ...HTML) HTML

func Noframes

func Noframes(attrs []a.Attribute, children ...HTML) HTML

func Noframes_

func Noframes_(children ...HTML) HTML

func Noscript

func Noscript(attrs []a.Attribute, children ...HTML) HTML

func Noscript_

func Noscript_(children ...HTML) HTML

func Object

func Object(attrs []a.Attribute, children ...HTML) HTML

func Object_

func Object_(children ...HTML) HTML

func Ol

func Ol(attrs []a.Attribute, children ...HTML) HTML

func Ol_

func Ol_(children ...HTML) HTML

func Optgroup

func Optgroup(attrs []a.Attribute, children ...HTML) HTML

func Optgroup_

func Optgroup_(children ...HTML) HTML

func Option

func Option(attrs []a.Attribute, children ...HTML) HTML

func Option_

func Option_(children ...HTML) HTML

func Output

func Output(attrs []a.Attribute, children ...HTML) HTML

func Output_

func Output_(children ...HTML) HTML

func P

func P(attrs []a.Attribute, children ...HTML) HTML

func P_

func P_(children ...HTML) HTML

func Param

func Param(attrs []a.Attribute) HTML

func Param_

func Param_() HTML

func Plaintext

func Plaintext(attrs []a.Attribute, children ...HTML) HTML

func Plaintext_

func Plaintext_(children ...HTML) HTML

func Pre

func Pre(attrs []a.Attribute, children ...HTML) HTML

func Pre_

func Pre_(children ...HTML) HTML

func Progress

func Progress(attrs []a.Attribute, children ...HTML) HTML

func Progress_

func Progress_(children ...HTML) HTML

func Q

func Q(attrs []a.Attribute, children ...HTML) HTML

func Q_

func Q_(children ...HTML) HTML

func Rp

func Rp(attrs []a.Attribute, children ...HTML) HTML

func Rp_

func Rp_(children ...HTML) HTML

func Rt

func Rt(attrs []a.Attribute, children ...HTML) HTML

func Rt_

func Rt_(children ...HTML) HTML

func Ruby

func Ruby(attrs []a.Attribute, children ...HTML) HTML

func Ruby_

func Ruby_(children ...HTML) HTML

func S

func S(attrs []a.Attribute, children ...HTML) HTML

func S_

func S_(children ...HTML) HTML

func Samp

func Samp(attrs []a.Attribute, children ...HTML) HTML

func Samp_

func Samp_(children ...HTML) HTML

func Script

func Script(attrs []a.Attribute, js JS) HTML

func Script_

func Script_(js JS) HTML

func Section

func Section(attrs []a.Attribute, children ...HTML) HTML

func Section_

func Section_(children ...HTML) HTML

func Select

func Select(attrs []a.Attribute, children ...HTML) HTML

func Select_

func Select_(children ...HTML) HTML

func Small

func Small(attrs []a.Attribute, children ...HTML) HTML

func Small_

func Small_(children ...HTML) HTML

func Source

func Source(attrs []a.Attribute) HTML

func Source_

func Source_() HTML

func Spacer

func Spacer(attrs []a.Attribute, children ...HTML) HTML

func Spacer_

func Spacer_(children ...HTML) HTML

func Span

func Span(attrs []a.Attribute, children ...HTML) HTML

func Span_

func Span_(children ...HTML) HTML

func Strike

func Strike(attrs []a.Attribute, children ...HTML) HTML

func Strike_

func Strike_(children ...HTML) HTML

func Strong

func Strong(attrs []a.Attribute, children ...HTML) HTML

func Strong_

func Strong_(children ...HTML) HTML

func Style

func Style(attrs []a.Attribute, children ...HTML) HTML

func Style_

func Style_(children ...HTML) HTML

func Sub

func Sub(attrs []a.Attribute, children ...HTML) HTML

func Sub_

func Sub_(children ...HTML) HTML

func Summary

func Summary(attrs []a.Attribute, children ...HTML) HTML

func Summary_

func Summary_(children ...HTML) HTML

func Sup

func Sup(attrs []a.Attribute, children ...HTML) HTML

func Sup_

func Sup_(children ...HTML) HTML

func Table

func Table(attrs []a.Attribute, children ...HTML) HTML

func Table_

func Table_(children ...HTML) HTML

func Tbody

func Tbody(attrs []a.Attribute, children ...HTML) HTML

func Tbody_

func Tbody_(children ...HTML) HTML

func Td

func Td(attrs []a.Attribute, children ...HTML) HTML

func Td_

func Td_(children ...HTML) HTML

func Text

func Text(v interface{}) HTML

Produce HTML from plain text by escaping

func Text_

func Text_(s string) HTML

func Textarea

func Textarea(attrs []a.Attribute, children ...HTML) HTML

func Textarea_

func Textarea_(children ...HTML) HTML

func Tfoot

func Tfoot(attrs []a.Attribute, children ...HTML) HTML

func Tfoot_

func Tfoot_(children ...HTML) HTML

func Th

func Th(attrs []a.Attribute, children ...HTML) HTML

func Th_

func Th_(children ...HTML) HTML

func Thead

func Thead(attrs []a.Attribute, children ...HTML) HTML

func Thead_

func Thead_(children ...HTML) HTML

func Time

func Time(attrs []a.Attribute, children ...HTML) HTML

func Time_

func Time_(children ...HTML) HTML

func Title

func Title(attrs []a.Attribute, children ...HTML) HTML

func Title_

func Title_(children ...HTML) HTML

func Tr

func Tr(attrs []a.Attribute, children ...HTML) HTML

func Tr_

func Tr_(children ...HTML) HTML

func Track

func Track(attrs []a.Attribute) HTML

func Track_

func Track_() HTML

func Tt

func Tt(attrs []a.Attribute, children ...HTML) HTML

func Tt_

func Tt_(children ...HTML) HTML

func U

func U(attrs []a.Attribute, children ...HTML) HTML

func U_

func U_(children ...HTML) HTML

func Ul

func Ul(attrs []a.Attribute, children ...HTML) HTML

func Ul_

func Ul_(children ...HTML) HTML

func Var

func Var(attrs []a.Attribute, children ...HTML) HTML

func Var_

func Var_(children ...HTML) HTML

func Video

func Video(attrs []a.Attribute, children ...HTML) HTML

func Video_

func Video_(children ...HTML) HTML

func VoidElement

func VoidElement(tag string, attrs []a.Attribute) HTML

func Wbr

func Wbr(attrs []a.Attribute) HTML

func Wbr_

func Wbr_() HTML

type JS

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

func JavaScript

func JavaScript(data interface{}, templs ...string) JS

func JavaScript_

func JavaScript_(templs ...string) JS

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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