Documentation
¶
Overview ¶
Package xhtml HTML 标签功能
Index ¶
- Variables
- func AddClass(w AttrsMapper, class ...string)
- func AddHTMLContent(w Container, txt string)
- func AddTextContent(w Container, txt string)
- func AddTo(to Container, children ...Element)
- func DeleteAttr(w AttrsMapper, key string, values ...string)
- func DeleteClass(w AttrsMapper, class ...string)
- func Dump(w io.Writer, obj any)
- func Render(e Element) template.HTML
- func SetAccept(w AttrsMapper, accept string)
- func SetAction(w AttrsMapper, action string)
- func SetAsync(w AttrsMapper)
- func SetAttr(w AttrsMapper, key string, value ...string)
- func SetAttrNoValue(w AttrsMapper, key string)
- func SetAutoComplete(w AttrsMapper, on bool)
- func SetChecked(w AttrsMapper, checked bool)
- func SetClass(w AttrsMapper, class ...string)
- func SetConst(key string, val any)
- func SetDisabled(w AttrsMapper, disabled bool)
- func SetFor(w AttrsMapper, f string)
- func SetForm(w AttrsMapper, form string)
- func SetFormAction(w AttrsMapper, formAction string)
- func SetFormTarget(w AttrsMapper, formAction string)
- func SetHeight(w AttrsMapper, height string)
- func SetHref(w AttrsMapper, href string)
- func SetID(w AttrsMapper, id string)
- func SetLang(w AttrsMapper, lang string)
- func SetList(w AttrsMapper, list string)
- func SetMax[N Number](w AttrsMapper, max N)
- func SetMaxLength(w AttrsMapper, maxLen int)
- func SetMethod(w AttrsMapper, method string)
- func SetMin[N Number](w AttrsMapper, min N)
- func SetMinLength(w AttrsMapper, minLen int)
- func SetName(w AttrsMapper, name string)
- func SetOnBlur(w AttrsMapper, script string)
- func SetOnChange(w AttrsMapper, script string)
- func SetOnClick(w AttrsMapper, script string)
- func SetOnFocus(w AttrsMapper, script string)
- func SetOnFormChange(w AttrsMapper, script string)
- func SetOnFormInput(w AttrsMapper, script string)
- func SetOnInput(w AttrsMapper, script string)
- func SetOnInvalid(w AttrsMapper, script string)
- func SetOnKeyDown(w AttrsMapper, script string)
- func SetOnKeyPress(w AttrsMapper, script string)
- func SetOnKeyUp(w AttrsMapper, script string)
- func SetOnReset(w AttrsMapper, script string)
- func SetOnSelect(w AttrsMapper, script string)
- func SetOnSubmit(w AttrsMapper, script string)
- func SetReadOnly(w AttrsMapper, readonly bool)
- func SetRequired(w AttrsMapper, required bool)
- func SetSelected(w AttrsMapper, checked bool)
- func SetSize(w AttrsMapper, size int)
- func SetSrc(w AttrsMapper, src string)
- func SetStep[N Number](w AttrsMapper, max N)
- func SetTarget(w AttrsMapper, target string)
- func SetTargetBlank(w AttrsMapper)
- func SetTitle(w AttrsMapper, title string)
- func SetType(w AttrsMapper, tp string)
- func SetValue(w AttrsMapper, value string)
- func SetWidth(w AttrsMapper, width string)
- func StripTags(input string) string
- func WalkParseFS(t *template.Template, fsys fs.FS, root string, patterns ...string) (*template.Template, error)
- func WithFuncMap(tpl *template.Template) *template.Template
- type Any
- func NewA(href string) *Any
- func NewAny(tag string) *Any
- func NewArticle() *Any
- func NewBody() *Any
- func NewButton(text string) *Any
- func NewCode() *Any
- func NewDL() *Any
- func NewDT() *Any
- func NewDatalist[T ~string](id string, ss []T) *Any
- func NewDiv() *Any
- func NewFieldSet() *Any
- func NewFigCaption() *Any
- func NewFigure() *Any
- func NewForm(method string, action string) *Any
- func NewHTML() *Any
- func NewHead() *Any
- func NewInput(tp string, name string) *Any
- func NewInputButton(value string) *Any
- func NewInputCheckbox(name string, value string, checked bool) *Any
- func NewInputColor(name string) *Any
- func NewInputData(name string) *Any
- func NewInputDateTimeLocal(name string) *Any
- func NewInputEmail(name string) *Any
- func NewInputMonth(name string) *Any
- func NewInputNumber(name string) *Any
- func NewInputPassword(name string) *Any
- func NewInputRadio(name string, value string, checked bool) *Any
- func NewInputRange[N Number](name string, min N, max N, step N) *Any
- func NewInputSearch(name string) *Any
- func NewInputSubmit(value string) *Any
- func NewInputText(name string) *Any
- func NewInputTime(name string) *Any
- func NewInputURL(name string) *Any
- func NewInputWeek(name string) *Any
- func NewLabel(html string) *Any
- func NewLegend(e Element) *Any
- func NewNav() *Any
- func NewOption(value string, b Element) *Any
- func NewP() *Any
- func NewPre() *Any
- func NewScript() *Any
- func NewSelect(name string, opts ...Element) *Any
- func NewStyle() *Any
- func NewTd(val ...Element) *Any
- func NewTh(val ...Element) *Any
- func NewTitle(c Element) *Any
- func WithAddress(es ...Element) *Any
- func WithAny(a *Any, fn func(*Any)) *Any
- func WithLabel(es ...Element) *Any
- type Attr
- type Attrs
- type AttrsMapper
- type Comment
- type Container
- type Element
- type ElementFunc
- type Elements
- type HTMLBytes
- type HTMLString
- type HTMLStringSlice
- type IMG
- type Link
- type Meta
- type Number
- type Pre
- type PreByte
- type StyleAttr
- func (s *StyleAttr) BackgroundColor(color string) *StyleAttr
- func (s *StyleAttr) Border(val string) *StyleAttr
- func (s *StyleAttr) Color(color string) *StyleAttr
- func (s *StyleAttr) Font(val string) *StyleAttr
- func (s *StyleAttr) FontFamily(val string) *StyleAttr
- func (s *StyleAttr) FontSize(val string) *StyleAttr
- func (s *StyleAttr) FontWeight(val string) *StyleAttr
- func (s *StyleAttr) Get(key string) string
- func (s *StyleAttr) HTML() ([]byte, error)
- func (s *StyleAttr) Height(h string) *StyleAttr
- func (s *StyleAttr) Margin(val string) *StyleAttr
- func (s *StyleAttr) MaxHeight(h string) *StyleAttr
- func (s *StyleAttr) MaxWidth(w string) *StyleAttr
- func (s *StyleAttr) MinHeight(h string) *StyleAttr
- func (s *StyleAttr) MinWidth(w string) *StyleAttr
- func (s *StyleAttr) Padding(val string) *StyleAttr
- func (s *StyleAttr) SetTo(a AttrsMapper) error
- func (s *StyleAttr) TextAlign(align string) *StyleAttr
- func (s *StyleAttr) Width(w string) *StyleAttr
- type TPLRequest
- func (t *TPLRequest) Context() context.Context
- func (t *TPLRequest) Dir() string
- func (t *TPLRequest) DirN(n int) string
- func (t *TPLRequest) EchoPathHas(sub string, echo any) any
- func (t *TPLRequest) EchoPathHasPrefix(prefix string, echo any) any
- func (t *TPLRequest) EchoPathHasSuffix(suffix string, echo any) any
- func (t *TPLRequest) EchoQueryEQ(field string, value any, echo any) any
- func (t *TPLRequest) Path() string
- func (t *TPLRequest) PathHas(sub string) bool
- func (t *TPLRequest) PathHasPrefix(prefix string) bool
- func (t *TPLRequest) PathHasSuffix(suffix string) bool
- func (t *TPLRequest) PathValue(name string) string
- func (t *TPLRequest) PathValueHas(name string) bool
- func (t *TPLRequest) Query(field string) string
- func (t *TPLRequest) QueryEQ(queryPair ...any) (bool, error)
- func (t *TPLRequest) QueryIn(field string, values []string) bool
- func (t *TPLRequest) WithNewQuery(queryPair ...any) (template.URL, error)
- func (t *TPLRequest) WithQuery(queryPair ...any) (template.URL, error)
- type Table1
- type TextBytes
- type TextString
- type TextStringSlice
- type WithAttrs
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // NL 换行: \n NL = HTMLBytes("\n") // BR HTML 换行 br BR = HTMLBytes("<br/>") // HR HTML 分割符 hr HR = HTMLBytes("<hr/>") )
var AdvancedFuncMap = map[string]func(tpl *template.Template) any{ "xRenderEscaped": func(tmpl *template.Template) any { return func(name string, values ...any) (template.HTML, error) { var data any switch len(values) { case 0: case 1: data = values[0] default: return "", errors.New("too many values") } buf := xsync.GetBytesBuffer() defer xsync.PutBytesBuffer(buf) if err := tmpl.ExecuteTemplate(buf, name, data); err != nil { return "", err } return template.HTML(html.EscapeString(buf.String())), nil } }, }
AdvancedFuncMap 支持在模版函数中读取到 Template 对象的更高级的辅助函数
var ErrEmptyTagName = errors.New("empty tag name")
ErrEmptyTagName tag 值为空的错误
var FuncMap = template.FuncMap{ "xRender": render, "xCheckedValue": tplfn.InputChecked, "xSelectedValue": tplfn.InputSelected, "xInputObjectName": tplfn.InputObjectName, "xRandStr": func() string { return xstr.RandNChar(8) }, "xRandStrN": xstr.RandNChar, "xRandIDN": xstr.RandIdentN, "xRandID": func() string { return xstr.RandIdentN(5) }, "xRandUint": rand.Uint, "xRandUint32": rand.Uint32, "xRandUint64": rand.Uint64, "xRandUintN": rand.UintN, "xRandUint32N": rand.Uint32N, "xRandUint64N": rand.Uint64N, "xRandInt": rand.Int, "xRandInt32": rand.Int32, "xRandInt64": rand.Int64, "xRandIntN": rand.IntN, "xRandInt32N": rand.Int32N, "xRandInt64N": rand.Int64N, "xRandFloat64": rand.Float64, "xRandFloat32": rand.Float32, "xNewMap": xmap.Create[string, any], "xMapKeys": tplfn.MapKeys, "xOrMap": tplfn.OrMap, "xDateTime": tplfn.DateTime, "xNowFormat": tplfn.NowTimeFormat, "xEachOfIter": tplfn.EachOfIter, "xRandOfIter": tplfn.RandOfIter, "xJSON": func(val any) (string, error) { bf, err := json.Marshal(val) return string(bf), err }, "xJSONIndent": func(val any) (string, error) { bf, err := json.MarshalIndent(val, "", " ") return string(bf), err }, "xDump": tplfn.Dump, "xIsOdd": tplfn.IsOddNumber, "xIsEven": tplfn.IsEvenNumber, "xModEQ": tplfn.IsRemainder, "xHTML": func(str string) template.HTML { return template.HTML(str) }, "xHTMLAttr": func(str string) template.HTMLAttr { return template.HTMLAttr(str) }, "xCss": func(str string) template.CSS { return template.CSS(str) }, "xJs": func(str string) template.JS { return template.JS(str) }, "xNewInts": func(start int, end int) []int { result := make([]int, 0, end-start) for i := start; i < end; i++ { result = append(result, i) } return result }, "xNewIntsStep": func(start int, end int, step int) []int { result := make([]int, 0, (end-start)/step+1) for i := start; i < end; i += step { result = append(result, i) } return result }, "xStrPrefix": strings.HasPrefix, "xStrSuffix": strings.HasSuffix, "xStrContains": strings.Contains, "xStrSplit": strings.Split, "xStrFields": strings.Fields, "xConst": getConst, "xAssert": tplfn.Assert, "xJoin": tplfn.Join, "xMathAdd": tplfn.MathAdd, "xMathSub": tplfn.MathSub, "xMathMul": tplfn.MathMul, "xMathDiv": tplfn.MathDiv, "xMathPercent": tplfn.MathPercent, "xMathComplement": tplfn.MathComplement, "xCat": func(items ...string) string { if len(items) == 0 { return "" } return strings.Join(items, "") }, "xToLower": strings.ToLower, "xToUpper": strings.ToUpper, "xToTitle": strings.ToTitle, "xTrimSpace": strings.TrimSpace, "xTrim": strings.Trim, "xnl2br": tplfn.NL2BR, "xMinLines": func(min int, str string) int { n := strings.Count(str, "\n") + 1 return max(min, n) }, "xPathDir": path.Dir, "xPathDirN": func(p string, n int) string { for range n { p = path.Dir(p) } return p }, "xPathClean": path.Clean, "xPathJoin": path.Join, "xPathBase": path.Base, "xPathIsAbs": path.IsAbs, "xTernary": func(ok bool, x any, y any) any { if ok { return x } return y }, }
FuncMap 用于模版的辅助方法
Functions ¶
func AddHTMLContent ¶
func AddTextContent ¶
func SetAttrNoValue ¶
func SetAttrNoValue(w AttrsMapper, key string)
SetAttrNoValue 设置只有 key,不需要 value 的属性
func SetAutoComplete ¶
func SetAutoComplete(w AttrsMapper, on bool)
SetAutoComplete 设置 autocomplete 属性
func SetFormAction ¶
func SetFormAction(w AttrsMapper, formAction string)
SetFormAction 设置 formaction 属性
func SetFormTarget ¶
func SetFormTarget(w AttrsMapper, formAction string)
SetFormTarget 设置 formtarget 属性,可用于 input type="submit"
func SetHref ¶
func SetHref(w AttrsMapper, href string)
func SetOnFormChange ¶
func SetOnFormChange(w AttrsMapper, script string)
SetOnFormChange 设置 onformchange 属性
func SetOnFormInput ¶
func SetOnFormInput(w AttrsMapper, script string)
SetOnFormInput 设置 onforminput 属性
func SetOnKeyPress ¶
func SetOnKeyPress(w AttrsMapper, script string)
SetOnKeyPress 设置 onkeypress 属性
func SetTargetBlank ¶
func SetTargetBlank(w AttrsMapper)
func WalkParseFS ¶
func WalkParseFS(t *template.Template, fsys fs.FS, root string, patterns ...string) (*template.Template, error)
WalkParseFS 遍历读取 fsys ,并将符合 pattern 的文件解析
pattern: 不能包含目录,有效值,如 *.html
注意:
- 所有 define 定义的块,全局应该不出现重名,在使用 template 方法渲染的时候,不应该添加其所在目录, 如在 user/index.html 文件中有 {{ define "status.part" }} Ok {{ end }}, 不管是在那个目录的那个文件,渲染该块,都不能添加目录: {{ template "status.part" .User }}
- 使用 template 渲染的时候,需要使用完整的路径,如 {{ template "user/index.html" . }}
Types ¶
type Any ¶
type Any struct {
// Tag 标签名称,必填,如 div
Tag string
// WithAttrs 可选,属性信息
WithAttrs
// Body 内容,可选
Body Elements
// SelfClose 当前标签是否自关闭,默认为 false
// 如 img 标签就是自关闭的:<img src="/a.jpg"/>
SelfClose bool
}
Any 一块 HTML 内容
func NewDatalist ¶
func NewHTML ¶
func NewHTML() *Any
NewHTML html 标签
Example ¶
package main
import (
"fmt"
"github.com/xanygo/anygo/xhtml"
)
func main() {
h := xhtml.NewHTML()
xhtml.AddTo(h,
xhtml.WithAny(xhtml.NewHead(), func(a *xhtml.Any) {
xhtml.AddTo(a, xhtml.NewTitle(xhtml.TextString("hello")))
}),
xhtml.WithAny(xhtml.NewBody(), func(a *xhtml.Any) {
xhtml.AddTo(a, xhtml.TextString("Hello World"))
}),
)
got, _ := h.HTML()
fmt.Println(string(got))
}
Output: <html><head><title>hello</title></head><body>Hello World</body></html>
func NewInputColor ¶
func NewInputData ¶
func NewInputDateTimeLocal ¶
NewInputDateTimeLocal 选择时间(无时区),得到如 2024-10-21T13:07
func NewInputEmail ¶
func NewInputMonth ¶
NewInputMonth 选择月份和年份,使用浏览器的日期选择器,选择后得到如 2024-10
func NewInputNumber ¶
func NewInputPassword ¶
func NewInputRange ¶
func NewInputSearch ¶
func NewInputText ¶
func NewOption ¶
NewOption 创建一个 option
Example ¶
package main
import (
"fmt"
"github.com/xanygo/anygo/xhtml"
)
func main() {
o1 := xhtml.NewOption("1", xhtml.TextString("class 1"))
got, _ := o1.HTML()
fmt.Println(string(got)) // <option value="1">class 1</option>
o2 := xhtml.NewOption("1", nil)
got2, _ := o2.HTML()
fmt.Println(string(got2)) // <option value="1">1</option>
}
Output: <option value="1">class 1</option> <option value="1">1</option>
func WithAddress ¶
type Attrs ¶
type Attrs struct {
// Sep 多个属性间的连接符,当为空时,使用默认值 " " (一个空格)
Sep string
// KVSep key 和 value 之间的连接符,当为空时,使用默认值 =
KVSep string
// contains filtered or unexported fields
}
Attrs 多个属性
type AttrsMapper ¶
AttrsMapper 具有 AttrsMapper 方法
type ElementFunc ¶
func (ElementFunc) HTML ¶
func (fn ElementFunc) HTML() ([]byte, error)
type HTMLBytes ¶
type HTMLBytes []byte
HTMLBytes 将 []byte 转换为 Element 类型,原样输出 HTML
type HTMLString ¶
type HTMLString string
HTMLString 将 String 转换为 Element 类型,原样输出 HTML
func (HTMLString) Div ¶
func (b HTMLString) Div() ([]byte, error)
func (HTMLString) Li ¶
func (b HTMLString) Li() ([]byte, error)
func (HTMLString) P ¶
func (b HTMLString) P() ([]byte, error)
func (HTMLString) Pre ¶
func (b HTMLString) Pre() ([]byte, error)
type HTMLStringSlice ¶
type HTMLStringSlice[T ~string] []T
HTMLStringSlice 将 []string 转换为 Element 类型
func (HTMLStringSlice[T]) Datalist ¶
func (ss HTMLStringSlice[T]) Datalist(id string) *Any
func (HTMLStringSlice[T]) Elements ¶
func (ss HTMLStringSlice[T]) Elements(tag string, fn func(b *Any)) Elements
Elements 转换为 字段 tag 的 []Element
func (HTMLStringSlice[T]) HTML ¶
func (ss HTMLStringSlice[T]) HTML() ([]byte, error)
func (HTMLStringSlice[T]) OL ¶
func (ss HTMLStringSlice[T]) OL() ([]byte, error)
func (HTMLStringSlice[T]) UL ¶
func (ss HTMLStringSlice[T]) UL() ([]byte, error)
type StyleAttr ¶
type StyleAttr struct {
WithAttrs
}
StyleAttr style 属性
func (*StyleAttr) BackgroundColor ¶
BackgroundColor 设置背景演示
func (*StyleAttr) FontFamily ¶
FontFamily 设置字体系列(字体族)
func (*StyleAttr) FontWeight ¶
FontWeight 设置字体粗细
type TPLRequest ¶
func NewTPLRequest ¶
func NewTPLRequest(req *http.Request) *TPLRequest
func (*TPLRequest) Context ¶
func (t *TPLRequest) Context() context.Context
func (*TPLRequest) DirN ¶
func (t *TPLRequest) DirN(n int) string
func (*TPLRequest) EchoPathHas ¶
func (t *TPLRequest) EchoPathHas(sub string, echo any) any
func (*TPLRequest) EchoPathHasPrefix ¶
func (t *TPLRequest) EchoPathHasPrefix(prefix string, echo any) any
func (*TPLRequest) EchoPathHasSuffix ¶
func (t *TPLRequest) EchoPathHasSuffix(suffix string, echo any) any
func (*TPLRequest) EchoQueryEQ ¶
func (t *TPLRequest) EchoQueryEQ(field string, value any, echo any) any
func (*TPLRequest) Path ¶
func (t *TPLRequest) Path() string
func (*TPLRequest) PathHas ¶
func (t *TPLRequest) PathHas(sub string) bool
func (*TPLRequest) PathHasPrefix ¶
func (t *TPLRequest) PathHasPrefix(prefix string) bool
func (*TPLRequest) PathHasSuffix ¶
func (t *TPLRequest) PathHasSuffix(suffix string) bool
func (*TPLRequest) PathValue ¶
func (t *TPLRequest) PathValue(name string) string
func (*TPLRequest) PathValueHas ¶
func (t *TPLRequest) PathValueHas(name string) bool
func (*TPLRequest) QueryEQ ¶
func (t *TPLRequest) QueryEQ(queryPair ...any) (bool, error)
QueryEQ 判断 Query 参数是否相等 参数 queryPair 必须是双数,依次为: 字段名,字段值,字段名,字段值 字段名必须是 string 类型
func (*TPLRequest) WithNewQuery ¶
func (t *TPLRequest) WithNewQuery(queryPair ...any) (template.URL, error)
WithNewQuery 基于当前 url 的 Path,生成新的链接
queryPair:url 中的 query 参数,必须成对出现,如 "a",1,"b","2","c","" 值为空的会忽略
type Table1 ¶
type Table1 struct {
WithAttrs
// contains filtered or unexported fields
}
Table1 一个简单的表格
type TextString ¶
type TextString string
TextString 文本,输出的时候会自动调用 html.EscapeString
func (TextString) Div ¶
func (b TextString) Div() ([]byte, error)
func (TextString) Li ¶
func (b TextString) Li() ([]byte, error)
func (TextString) P ¶
func (b TextString) P() ([]byte, error)
func (TextString) Pre ¶
func (b TextString) Pre() ([]byte, error)
type TextStringSlice ¶
type TextStringSlice []string
TextStringSlice 将 []string 转换为 Element 类型
func (TextStringSlice) Datalist ¶
func (ss TextStringSlice) Datalist(id string) *Any
func (TextStringSlice) Elements ¶
func (ss TextStringSlice) Elements(tag string, fn func(b *Any)) Elements
Elements 转换为 字段 tag 的 []Element
func (TextStringSlice) HTML ¶
func (ss TextStringSlice) HTML() ([]byte, error)
func (TextStringSlice) OL ¶
func (ss TextStringSlice) OL() ([]byte, error)
func (TextStringSlice) UL ¶
func (ss TextStringSlice) UL() ([]byte, error)
Example ¶
package main
import (
"fmt"
"github.com/xanygo/anygo/xhtml"
)
func main() {
values := xhtml.TextStringSlice{"1", "2", "3"}
got, _ := values.UL()
fmt.Println(string(got))
}
Output: <ul><li>1</li><li>2</li><li>3</li></ul>