dhtml

package module
v0.9.1-beta Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2025 License: MIT Imports: 11 Imported by: 4

README

dhtml - Go html renderer

Reference GitHub code size Go Report Card GitHub

GitHub Version GitHub Release GitHub commit activity

Go html renderer inspired by Drupal's renderable arrays concept. Allows to built some elements tree in Go and then render it as html.

Ready to try beta version.

Code Example:

//Build elements tree, <html> as root element
html := dhtml.Tag("html").
  Comment("some <html> escaped comment").
  //appending children elements
  Append(
    dhtml.Tag("head").
      Append(
        //element with attributes
        dhtml.Tag("link").Attribute("href", "/assets/vendor/bootstrap.min.css").Attribute("rel", "stylesheet"),
      ),
  ).
  //body element
  Append(dhtml.Tag("body").
    Append(
      // dhtml.Div() is a shorthand for dhtml.Tag("div")
      dhtml.Div().Id("test").
        Attribute("data-mt-test", "some attribute").
        //classes deduplication
        Class("border").Class("m-3").Class("p-3").
        Content("some <escaped> text"),
    ),
    Append(
      dhtml.Div()
        //multiple classes
        Classes([]string{"border", "border-danger", "border-5"}).
        Content("another text in red rectangle"),
    ),
  )

//return it as HTML string
return html.String()

And yes, one more time: Thank you, Drupal!

Documentation

Index

Constants

View Source
const (
	TAG_NAME_REGEXP       = `^[a-z][a-z0-9\-]{0,255}$`
	ATTRIBUTE_NAME_REGEXP = `^[a-z][a-z0-9\-\:]{0,255}$`
	HTML_ID_REGEXP        = `^[a-z][a-z0-9\_]{0,255}$`
	CLASS_NAME_REGEXP     = `^\-?[\_a-z][\_\-a-z0-9\-\:]{0,255}$`
)

Variables

This section is empty.

Functions

func AnyToClasslist

func AnyToClasslist(v any) []string

CSS-classes string (or something stringable) "parser"

func CheckAttributeName

func CheckAttributeName(name string) error

func CheckClassName

func CheckClassName(name string) error

func CheckId

func CheckId(s string) error

func CheckTagName

func CheckTagName(name string) error

func SafeAttributeName

func SafeAttributeName(name string) string

func SafeClassName

func SafeClassName(name string) string

func SafeId

func SafeId(s string) string

func SafeTagName

func SafeTagName(name string) string

func Settings

func Settings() *settingsType

Types

type Classes

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

CSS classes list handling

func NewClasses

func NewClasses(v ...any) (c Classes)

func (*Classes) Add

func (c *Classes) Add(v ...any) *Classes

func (*Classes) AddFromSet

func (c *Classes) AddFromSet(class_set []string, v ...any) *Classes

Adds class(es) from v if no classes from class_set already added

func (*Classes) Count

func (c *Classes) Count() int

func (*Classes) GetClassList

func (c *Classes) GetClassList() []string

func (*Classes) Prepend

func (c *Classes) Prepend(v any) *Classes

func (*Classes) String

func (c *Classes) String() string

type ConfirmLinkElement

type ConfirmLinkElement struct {
	LinkElement
}

<a> tag with onclick="return confirm('message');"

func NewConfirmLink(href, confirmMessage string) *ConfirmLinkElement

Html form just to render it

func (*ConfirmLinkElement) GetTags

func (e *ConfirmLinkElement) GetTags() TagList

type DebugElement

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

func NewDebugElement

func NewDebugElement(skip int) *DebugElement

func (*DebugElement) Append

func (e *DebugElement) Append(v any) *DebugElement

func (*DebugElement) GetTags

func (e *DebugElement) GetTags() TagList

func (*DebugElement) Textf

func (e *DebugElement) Textf(format string, a ...any) *DebugElement

type ElementI

type ElementI interface {
	GetTags() TagList
}

Element is something that can be turned in list of html tags. Very simple elements are: tags itself, html comments or just plain text content. It could be much more complex things like Bootstrap's card for example. Whole HTML document is element as well (see dhtml.Document helper).

func AnyToElement

func AnyToElement(v any) ElementI

func Dbg

func Dbg(format string, a ...any) ElementI

type ElementWalkFunc

type ElementWalkFunc func(e ElementI, args ...any)

Function to be passed to Walk() or WalkR() methods

type FormElement

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

func NewForm

func NewForm() *FormElement

Html form just to render it

func (*FormElement) Append

func (f *FormElement) Append(v ...any) *FormElement

func (*FormElement) Class

func (f *FormElement) Class(v ...any) *FormElement

func (*FormElement) GetTags

func (f *FormElement) GetTags() TagList

func (*FormElement) Method

func (f *FormElement) Method(method string) *FormElement

type HtmlDocument

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

func NewHtmlDocument

func NewHtmlDocument() *HtmlDocument

func (*HtmlDocument) Body

func (d *HtmlDocument) Body() *Tag

func (*HtmlDocument) Charset

func (d *HtmlDocument) Charset(charset string) *HtmlDocument

func (*HtmlDocument) GetTags

func (d *HtmlDocument) GetTags() TagList

func (*HtmlDocument) Head

func (d *HtmlDocument) Head() *Tag

func (*HtmlDocument) Icon

func (d *HtmlDocument) Icon(icon string) *HtmlDocument

func (*HtmlDocument) String

func (d *HtmlDocument) String() string

func (*HtmlDocument) Stylesheet

func (d *HtmlDocument) Stylesheet(href string) *HtmlDocument

func (*HtmlDocument) Title

func (d *HtmlDocument) Title(title string) *HtmlDocument

type HtmlPiece

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

HtmlPiece is set of one or several html elements (or no elements at all). Could be tags, complex elements, text content etc. Every HtmlPiece as an element itself (so it can be rendered as HTML).

func NewHtmlPiece

func NewHtmlPiece() *HtmlPiece

Actual Constructor

func Piece

func Piece(firstElement any) *HtmlPiece

If firstElement is HtmlPiece, return it. Else create new HtmlPiece and add firstElement to its contents.

func (*HtmlPiece) Append

func (p *HtmlPiece) Append(v ...any) *HtmlPiece

Adds something to the piece: another piece, ElemenetI, any string, Stringer or other value.

func (*HtmlPiece) AppendElement

func (p *HtmlPiece) AppendElement(e ElementI) *HtmlPiece

Adds single element

func (*HtmlPiece) AppendPiece

func (p *HtmlPiece) AppendPiece(another_piece *HtmlPiece) *HtmlPiece

Adds another piece elements to this one

func (*HtmlPiece) AppendText

func (p *HtmlPiece) AppendText(text string) *HtmlPiece

Adds text element to piece

func (*HtmlPiece) Clear

func (p *HtmlPiece) Clear() *HtmlPiece

Remove all contents and cached rendered tags.

func (*HtmlPiece) GetTags

func (p *HtmlPiece) GetTags() TagList

ElementI implementation

func (*HtmlPiece) IsEmpty

func (p *HtmlPiece) IsEmpty() bool

Returns true if piece has no anything added to it

func (*HtmlPiece) Len

func (p *HtmlPiece) Len() int

Elements count

func (*HtmlPiece) RawString

func (p *HtmlPiece) RawString() string

Render just text content only

func (*HtmlPiece) String

func (p *HtmlPiece) String() string

Render everything to string as HTML

func (*HtmlPiece) Textf

func (p *HtmlPiece) Textf(format string, a ...any) *HtmlPiece

Format and add text element

func (*HtmlPiece) Walk

func (p *HtmlPiece) Walk(f ElementWalkFunc, args ...any)

Calls f function for each element.

func (*HtmlPiece) WalkR

func (p *HtmlPiece) WalkR(f ElementWalkFunc, args ...any)

Calls f function for each element with recursion.

type LabelElement

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

simple <label> element

func NewLabel

func NewLabel() *LabelElement

func (*LabelElement) Append

func (e *LabelElement) Append(v ...any) *LabelElement

<label> contents

func (*LabelElement) Class

func (e *LabelElement) Class(v ...any) *LabelElement

func (*LabelElement) For

func (e *LabelElement) For(targetId string) *LabelElement

func (*LabelElement) GetTags

func (e *LabelElement) GetTags() TagList

func (*LabelElement) Styles

func (e *LabelElement) Styles(v ...any) *LabelElement

type LinkElement

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

simple <a> element

func NewLink(href string) *LinkElement

func (*LinkElement) Class

func (e *LinkElement) Class(v ...any) *LinkElement

func (*LinkElement) GetTags

func (e *LinkElement) GetTags() TagList

func (*LinkElement) Label

func (e *LinkElement) Label(v any) *LinkElement

func (*LinkElement) Target

func (e *LinkElement) Target(target string) *LinkElement

func (*LinkElement) Title

func (e *LinkElement) Title(title string) *LinkElement

type ListElement

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

simple <ul> and <ol> elements

func NewOrderedList

func NewOrderedList() *ListElement

func NewUnorderedList

func NewUnorderedList() *ListElement

func (*ListElement) AppendItem

func (e *ListElement) AppendItem(item *ListItemElement) *ListElement

func (*ListElement) Class

func (e *ListElement) Class(v ...any) *ListElement

func (*ListElement) GetTags

func (e *ListElement) GetTags() TagList

func (*ListElement) Item

func (e *ListElement) Item(v ...any) *ListItemElement

func (*ListElement) ItemCount

func (e *ListElement) ItemCount() int

type ListItemElement

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

simple <li> element

func NewListItem

func NewListItem() *ListItemElement

func (*ListItemElement) Append

func (e *ListItemElement) Append(v ...any) *ListItemElement

func (*ListItemElement) Class

func (e *ListItemElement) Class(v ...any) *ListItemElement

func (*ListItemElement) GetTags

func (e *ListItemElement) GetTags() TagList

type NamedHtmlPieces

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

Set of named html pieces

func NewNamedHtmlPieces

func NewNamedHtmlPieces() NamedHtmlPieces

func (*NamedHtmlPieces) Add

func (np *NamedHtmlPieces) Add(name string, v any)

func (*NamedHtmlPieces) Clear

func (np *NamedHtmlPieces) Clear()

func (*NamedHtmlPieces) Get

func (np *NamedHtmlPieces) Get(name string) *HtmlPiece

func (*NamedHtmlPieces) GetOk

func (np *NamedHtmlPieces) GetOk(name string) (p *HtmlPiece, ok bool)

func (*NamedHtmlPieces) IsEmpty

func (np *NamedHtmlPieces) IsEmpty(name string) bool

func (*NamedHtmlPieces) Set

func (np *NamedHtmlPieces) Set(name string, v any)

type OptionElement

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

==================== OptionElement ===================

func NewOption

func NewOption() *OptionElement

func (*OptionElement) Body

func (c *OptionElement) Body(v any) *OptionElement

func (*OptionElement) GetTags

func (c *OptionElement) GetTags() TagList

region Rendering

func (*OptionElement) Selected

func (c *OptionElement) Selected(b bool) *OptionElement

func (*OptionElement) Value

func (c *OptionElement) Value(v any) *OptionElement

type RenderFunc

type RenderFunc func() HtmlPiece

Function returning some html.

type SelectElement

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

func NewSelect

func NewSelect() *SelectElement

func (*SelectElement) AppendOption

func (c *SelectElement) AppendOption(option *OptionElement) *SelectElement

func (*SelectElement) Attribute

func (c *SelectElement) Attribute(name, value string) *SelectElement

func (*SelectElement) Class

func (c *SelectElement) Class(v any) *SelectElement

func (*SelectElement) GetTags

func (c *SelectElement) GetTags() TagList

func (*SelectElement) Id

func (c *SelectElement) Id(id string) *SelectElement

func (*SelectElement) Option

func (c *SelectElement) Option(value any, body any) *OptionElement

type Styles

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

styles list handling

func NewStyles

func NewStyles(v ...any) Styles

Creates new Styles{} (with possible initial values).

func (*Styles) Add

func (st *Styles) Add(v ...any) *Styles

string (or stringable) "parser"

func (*Styles) Clear

func (st *Styles) Clear() *Styles

func (*Styles) Count

func (st *Styles) Count() int

func (*Styles) Get

func (st *Styles) Get(property string) string

func (*Styles) Set

func (st *Styles) Set(property, value string) *Styles

func (*Styles) String

func (s *Styles) String() string

type TableCellElement

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

func NewTableCell

func NewTableCell() *TableCellElement

func (*TableCellElement) Append

func (e *TableCellElement) Append(v any) *TableCellElement

func (*TableCellElement) Class

func (e *TableCellElement) Class(v ...any) *TableCellElement

func (*TableCellElement) GetTags

func (e *TableCellElement) GetTags() TagList

type TableElement

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

simple <table> element

func NewTable

func NewTable() *TableElement

func (*TableElement) AppendRow

func (e *TableElement) AppendRow(row *TableRowElement) *TableElement

Adds <TR> to the table data

func (*TableElement) Class

func (e *TableElement) Class(v ...any) *TableElement

func (*TableElement) EmptyLabel

func (e *TableElement) EmptyLabel(label string) *TableElement

func (*TableElement) GetTags

func (e *TableElement) GetTags() TagList

func (*TableElement) Header

func (e *TableElement) Header(v any) *TableElement

Adds header value (<tr><th>v</th></tr>) to the table

func (*TableElement) NewRow

func (e *TableElement) NewRow() (row *TableRowElement)

Creates new row, appends it to table and returns back

func (*TableElement) RowCount

func (e *TableElement) RowCount() int

Returns added rows count.

type TableRowElement

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

func NewTableRow

func NewTableRow() *TableRowElement

func (*TableRowElement) AppendCell

func (e *TableRowElement) AppendCell(cell *TableCellElement) *TableRowElement

Add <TD> to the row

func (*TableRowElement) Cell

func (e *TableRowElement) Cell(v any) *TableCellElement

Add new <TD> with given content to the row

func (*TableRowElement) Class

func (e *TableRowElement) Class(v ...any) *TableRowElement

func (*TableRowElement) GetTags

func (e *TableRowElement) GetTags() TagList

type Tag

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

Basic tag element implementation

func Comment

func Comment(text string) *Tag

func Div

func Div() *Tag

func EmptyLabel

func EmptyLabel(label string) *Tag

func NewTag

func NewTag(tag string) *Tag

Tag constructor

func RenderValue

func RenderValue(title, value any) *Tag

Renders title and some value

func RenderValueE

func RenderValueE(title, value any, emptyLabel string) *Tag

Renders title and some value. If value is empty, render EmptyLabel instead

func Span

func Span() *Tag

func Text

func Text(text string) *Tag

func Textf

func Textf(format string, args ...any) *Tag

func UnsafeText

func UnsafeText(text string) *Tag

func (*Tag) Append

func (t *Tag) Append(v ...any) *Tag

Adds child element(s)

func (*Tag) Attribute

func (e *Tag) Attribute(name, value string) *Tag

Sets attribute.

func (*Tag) AttributeUnsafe

func (e *Tag) AttributeUnsafe(name, unsafeValue string) *Tag

func (*Tag) ChildrenCount

func (e *Tag) ChildrenCount() int

func (*Tag) Class

func (e *Tag) Class(v ...any) *Tag

Adds one or more CSS classes.

func (*Tag) Comment

func (e *Tag) Comment(text string) *Tag

Adds html comment as a child to the element

func (*Tag) GetAttribute

func (e *Tag) GetAttribute(name string) string

func (*Tag) GetClasses

func (e *Tag) GetClasses() *Classes

func (*Tag) GetTags

func (t *Tag) GetTags() TagList

func (*Tag) HasChildren

func (e *Tag) HasChildren() bool

func (*Tag) Id

func (e *Tag) Id(id string) *Tag

func (*Tag) IsComment

func (e *Tag) IsComment() bool

func (*Tag) IsInline

func (t *Tag) IsInline() bool

true if this tag could be rendered inline, false - should be rendered on new line and indented.

func (*Tag) IsText

func (e *Tag) IsText() bool

func (*Tag) IsUnsafeText

func (e *Tag) IsUnsafeText() bool

func (*Tag) String

func (t *Tag) String() string

Renders element with all the children as HTML

func (*Tag) Style

func (e *Tag) Style(property, value string) *Tag

Set one CSS-style property.

func (*Tag) Styles

func (e *Tag) Styles(v ...any) *Tag

Set one or more CSS-style properties from string, []string, Stringer etc.

func (*Tag) Text

func (e *Tag) Text(content string) *Tag

func (*Tag) Textf

func (e *Tag) Textf(format string, a ...any) *Tag

func (*Tag) Title

func (e *Tag) Title(s string) *Tag

func (*Tag) Walk

func (t *Tag) Walk(f ElementWalkFunc, args ...any)

Calls f for each child element

func (*Tag) WalkR

func (t *Tag) WalkR(f ElementWalkFunc, args ...any)

Calls f for each child element with recursion

type TagList

type TagList []*Tag

Jump to

Keyboard shortcuts

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