Documentation ¶
Overview ¶
Package m is an HTML element builder and renderer, inspired by Mithril.js.
Index ¶
- func Render(w io.Writer, element Element) error
- func RenderString(element Element) string
- type Element
- func Attr(key, value string) Element
- func Attrf(key, valueFormat string, x ...interface{}) Element
- func Document(elements ...Element) Element
- func F(format string, x ...interface{}) Element
- func For(start, end, step int, fn func(i int) Element) Element
- func Group(n int, group func(i, j int) bool, render func(i, j int) Element) Element
- func If(cond bool, ifTrue Element) Element
- func IfElse(cond bool, ifTrue, ifFalse Element) Element
- func M(selector string, elements ...Element) Element
- func Range(n int, fn func(i int) Element) Element
- func Raw(html string) Element
- func S(elements ...Element) Element
- func T(text string) Element
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Render ¶
Render writes the HTML of element to w.
A non-nil error is returned if the element could not be successfully written.
Example ¶
el := M("p", T("Hello World"), ) if err := Render(os.Stdout, el); err != nil { panic(err) }
Output: <p>Hello World</p>
func RenderString ¶
RenderString returns the HTML of element.
Example ¶
el := M("p", T("Hello World"), ) fmt.Println(RenderString(el))
Output: <p>Hello World</p>
Types ¶
type Element ¶
type Element interface { // Element returns the Element to be rendered. // // In most cases, it will return an element constructed with one of // the functions in this package. Element() Element }
Element is an object that can be rendered as HTML.
Example ¶
package main import ( "fmt" . "layeh.com/m" ) type Alert struct { child Element } func NewAlert(child Element) *Alert { return &Alert{ child: child, } } func (e *Alert) Element() Element { return M("div.alert.alert-primary[role=alert]", e.child, ) } func main() { el := NewAlert(T("Access Denied")) fmt.Println(RenderString(el)) }
Output: <div class="alert alert-primary" role="alert">Access Denied</div>
func Attr ¶
Attr returns an HTML element attribute with the given key and value.
The return value is only valid when used as the first elements in calling M.
Example ¶
el := M("p", Attr("data-id", "ff34"), T("User"), ) fmt.Println(RenderString(el))
Output: <p data-id="ff34">User</p>
func Attrf ¶
Attrf returns an HTML element attribute with the given key and value. The value is formatted using fmt.
The return value is only valid when used as the first elements in calling M.
Example ¶
el := M("div", Attrf("id", "user-%d", 10), T("User section"), ) fmt.Println(RenderString(el))
Output: <div id="user-10">User section</div>
func Document ¶
Document returns an element that renders the HTML5 doctype before elements.
Example ¶
doc := Document( M("head", M("title", T("Hello World")), ), M("body"), ) if err := Render(os.Stdout, doc); err != nil { panic(err) }
Output: <!DOCTYPE html> <head><title>Hello World</title></head><body></body>
func F ¶
F returns an escaped text element that is formatted using fmt.
Example ¶
el := M("p", F("Hello, %s", "World"), ) fmt.Println(RenderString(el))
Output: <p>Hello, World</p>
func For ¶
For returns an element that is called for every index of the loop with the given conditions.
Example ¶
el := M("ul", For(0, 3, 1, func(i int) Element { return M("li", F("%d", i*10)) }), ) fmt.Println(RenderString(el))
Output: <ul><li>0</li><li>10</li><li>20</li></ul>
func Group ¶
Group returns an element that renders contiguous values that match the group function.
From 0 to N (i), the group function is evaluated
group(i-1, i)
If it returns false, the render function is called with the lowest index not yet rendered and the current index. If it returns true, the current index is incremented. Render is called at the end if any indices remain not yet rendered.
Example ¶
users := []string{ "Alice", "Bob", "Bill", "Eve", } el := Group(len(users), func(i, j int) bool { // Group users by first letter of name return users[i][0] == users[j][0] }, func(i, j int) Element { return M("p", T(strings.Join(users[i:j], ", ")), ) }) fmt.Println(RenderString(el))
Output: <p>Alice</p><p>Bob, Bill</p><p>Eve</p>
func If ¶
If returns ifTrue if cond is true, nil otherwise.
Example ¶
el := Range(5, func(i int) Element { return M("p", F("%d", i), If(i%2 == 0, T("!")), ) }) fmt.Println(RenderString(el))
Output: <p>0!</p><p>1</p><p>2!</p><p>3</p><p>4!</p>
func IfElse ¶
IfElse returns ifTrue or ifFalse, depending on the value of cond.
Example ¶
el := Range(5, func(i int) Element { return M("p", F("%d", i), IfElse(i%2 == 0, T("!"), T("?")), ) }) fmt.Println(RenderString(el))
Output: <p>0!</p><p>1?</p><p>2!</p><p>3?</p><p>4!</p>
func M ¶
M returns an element that is an HTML tag that is specified by selector.
Selectors are defined using the following syntax:
tagname#id.class-1.class-2[attr-key-1=value][attr-key-2=value]
The element ID (#), class names (.), and attributes ([]) are optional. Multiple class names and attributes can be defined, but only one element ID. If tagname is not defined, div is used.
The selector should be a constant value. If dynamic values are required for an ID, class name, or attribute, omit the dynamic value from the selector string and use Attr or Attrf instead.
Multiple class attributes are merged together into a space-separated string.
elements are the children of the HTML tag. If Attr and Attrf values are used, they must be the first values included in elements.
The function panics on an invalid selector.
Example ¶
el := M("h1#headline.active.etc[data-id=3]", T("Hello World"), ) fmt.Println(RenderString(el))
Output: <h1 id="headline" class="active etc" data-id="3">Hello World</h1>