Documentation
¶
Index ¶
- type Node
- func Combine(options ...Node) Node
- func Document(options ...Node) Node
- func ForEach[T any](items []T, view func(T) Node) Node
- func InnerText(content string) Node
- func NewAttribute(name string, value string) Node
- func NewBoolAttribute(name string) Node
- func NewTag(name string, options ...Node) Node
- func NewVoidTag(name string, options ...Node) Node
- func RawInnerText(content string) Node
- type Visitor
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Node ¶
type Node struct {
// contains filtered or unexported fields
}
Node represents an HTML tag or attribute. It is the core type of the sanity library. Nodes are immutable and may be safely shared across threads.
- The `tags` package contains constructors for all standard HTML5 tags.
- The `attr` package contains constructors for all standard HTML5 attributes.
- The `html` package containing the Node implementation contains generic constructors and utilities.
func Combine ¶
Combine multiple Nodes into a single Node. This can be useful for grouping nodes without adding a wrapping element.
Example Usage:
c := Combine(tag.Div(InnerText("hello")), tag.Div(InnerText("world"))) c.String() == "<div>hello</div><div>world</div>"
func Document ¶
Document renders the preamble required by the HTML standard.
html.Document(attr.Lang("en"), html.InnerText("Hello World!")) would render as: <!DOCTYPE html> <html lang="en">Hello world!</html>
func ForEach ¶
ForEach applies the view function to each item, then returns a node containing the combined results.
Example Usage:
fruits := []string{"apple", "bannana", "orange"} list := tag.Ul(ForEach(fruits, func(fruit string) Node { return tag.Li(InnerText(fruit)) }) list.String() == "<ul><li>apple</li><li>bannana</li><li>orange</li></ul>"
func InnerText ¶
InnerText is rendered as text inside a tag. HTML in the content is escaped.
Example Usage: node := tag.Div(InnerText("hello world!")) node.String() == "<div>hello world!</div>"
func NewAttribute ¶
NewAttribute creates an attribute with a value. Like id="some-id" or class="class-a class-b".
func NewBoolAttribute ¶
NewBoolAttribute creates a bool attribute. A bool attribute is an attribute with no value. An example bool attribute is the `disabled` attribute in <button disabled>Submit</button>.
func NewVoidTag ¶
NewVoidTag creates an element that has no closing tag. Like <img> or <input>.
func RawInnerText ¶
RawInnerText is rendered as text inside a tag. Danger: HTML in the content is not escaped.
Example Usage: node := tag.Div(RawInnerText("<script>alert("hello world")</script>")) node.String() == "<div>"<script>alert("hello world")</script>"</div>"
func (Node) Render ¶
Render converts the node and all of its children into a byte array. Writing the byte array to an io.Writer is slightly more efficient than writing a string.
Example Usage: writer.Write(node.Render())
func (*Node) Visit ¶ added in v0.0.2
Visit walks the Node tree. It is used by Node.Render to convert the Node tree to HTML.
func (*Node) VisitAttributes ¶ added in v0.0.2
VisitAttributes visits the node's attributes. This should be used by the visitor.Tag and visitor.VoidTag implementations to visit the tag's attributes.
func (*Node) VisitChildren ¶ added in v0.0.2
VisitChildren visits the node's children. This should be used by visitor.Tag implementation to visit tags and inner HTML contained within the tag.
type Visitor ¶ added in v0.0.2
type Visitor interface { Tag(name string, node *Node) VoidTag(name string, node *Node) Content(content string) Attribute(name string, value *string) }
Visitor makes it possible to walk the Node tree. See `render.go` for an example visitor implementation. Visitor is used to implement node.Render.