m

package module
v0.0.0-...-c8d14b3 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2020 License: Unlicense Imports: 6 Imported by: 0

README

m GoDoc

Package m is an HTML element builder and renderer, inspired by Mithril.js.

package m_test

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 ExampleElement() {
	el := NewAlert(T("Access Denied"))
	fmt.Println(RenderString(el))
	// Output:
	// <div class="alert alert-primary" role="alert">Access Denied</div>
}

License

Public domain

Author

Tim Cooper (tim.cooper@layeh.com)

Documentation

Overview

Package m is an HTML element builder and renderer, inspired by Mithril.js.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Render

func Render(w io.Writer, element Element) error

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

func RenderString(element Element) string

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

func Attr(key, value string) Element

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

func Attrf(key, valueFormat string, x ...interface{}) Element

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

func Document(elements ...Element) Element

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

func F(format string, x ...interface{}) Element

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

func For(start, end, step int, fn func(i int) Element) Element

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

func Group(n int, group func(i, j int) bool, render func(i, j int) Element) Element

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

func If(cond bool, ifTrue Element) Element

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

func IfElse(cond bool, ifTrue, ifFalse Element) Element

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

func M(selector string, elements ...Element) Element

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>

func Range

func Range(n int, fn func(i int) Element) Element

Range returns an element that is called for every index from 0 to n.

func Raw

func Raw(html string) Element

Raw returns an element that renders the given HTML unescaped.

func S

func S(elements ...Element) Element

S returns an element where each elements are concatenated together.

func T

func T(text string) Element

T returns an escaped text element.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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