xhtml

package
v0.0.0-...-78a0eeb Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2026 License: BSD-3-Clause Imports: 28 Imported by: 1

Documentation

Overview

Package xhtml HTML 标签功能

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// NL 换行: \n
	NL = HTMLBytes("\n")

	// BR HTML 换行 br
	BR = HTMLBytes("<br/>")

	// HR HTML 分割符 hr
	HR = HTMLBytes("<hr/>")
)
View Source
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 对象的更高级的辅助函数

View Source
var ErrEmptyTagName = errors.New("empty tag name")

ErrEmptyTagName tag 值为空的错误

View Source
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 AddClass

func AddClass(w AttrsMapper, class ...string)

AddClass 添加 class 属性

func AddHTMLContent

func AddHTMLContent(w Container, txt string)

func AddTextContent

func AddTextContent(w Container, txt string)

func AddTo

func AddTo(to Container, children ...Element)

AddTo 给指定的对象添加子元素

func DeleteAttr

func DeleteAttr(w AttrsMapper, key string, values ...string)

DeleteAttr 删除指定的属性值

func DeleteClass

func DeleteClass(w AttrsMapper, class ...string)

DeleteClass 删除 class 属性

func Dump

func Dump(w io.Writer, obj any)

func Render

func Render(e Element) template.HTML

func SetAccept

func SetAccept(w AttrsMapper, accept string)

SetAccept 设置 accept 属性

func SetAction

func SetAction(w AttrsMapper, action string)

SetAction 设置 action 属性

func SetAsync

func SetAsync(w AttrsMapper)

SetAsync 设置 async 属性

func SetAttr

func SetAttr(w AttrsMapper, key string, value ...string)

SetAttr 设置属性值

func SetAttrNoValue

func SetAttrNoValue(w AttrsMapper, key string)

SetAttrNoValue 设置只有 key,不需要 value 的属性

func SetAutoComplete

func SetAutoComplete(w AttrsMapper, on bool)

SetAutoComplete 设置 autocomplete 属性

func SetChecked

func SetChecked(w AttrsMapper, checked bool)

SetChecked 设置 form 属性

func SetClass

func SetClass(w AttrsMapper, class ...string)

SetClass 设置 class 属性

func SetConst

func SetConst(key string, val any)

SetConst 定义/存储一个值,用于在模版中使用 xConst 方法读取到

func SetDisabled

func SetDisabled(w AttrsMapper, disabled bool)

SetDisabled 设置 disabled 属性

func SetFor

func SetFor(w AttrsMapper, f string)

SetFor 设置 for 属性

func SetForm

func SetForm(w AttrsMapper, form string)

SetForm 设置 form 属性

func SetFormAction

func SetFormAction(w AttrsMapper, formAction string)

SetFormAction 设置 formaction 属性

func SetFormTarget

func SetFormTarget(w AttrsMapper, formAction string)

SetFormTarget 设置 formtarget 属性,可用于 input type="submit"

func SetHeight

func SetHeight(w AttrsMapper, height string)

SetHeight 设置元素的 height

func SetHref

func SetHref(w AttrsMapper, href string)

func SetID

func SetID(w AttrsMapper, id string)

SetID 设置元素的 id

func SetLang

func SetLang(w AttrsMapper, lang string)

SetLang 设置元素的 lang 属性

如 en-US、zh-CN

func SetList

func SetList(w AttrsMapper, list string)

SetList 设置 list 属性

func SetMax

func SetMax[N Number](w AttrsMapper, max N)

SetMax 设置 max 属性

func SetMaxLength

func SetMaxLength(w AttrsMapper, maxLen int)

SetMaxLength 设置 maxlength 属性

func SetMethod

func SetMethod(w AttrsMapper, method string)

SetMethod 设置 method 属性

func SetMin

func SetMin[N Number](w AttrsMapper, min N)

SetMin 设置 min 属性

func SetMinLength

func SetMinLength(w AttrsMapper, minLen int)

SetMinLength 设置 minlength 属性

func SetName

func SetName(w AttrsMapper, name string)

SetName 设置元素的 name

func SetOnBlur

func SetOnBlur(w AttrsMapper, script string)

SetOnBlur 设置 onblur 属性

func SetOnChange

func SetOnChange(w AttrsMapper, script string)

SetOnChange 设置 onchange 属性

func SetOnClick

func SetOnClick(w AttrsMapper, script string)

SetOnClick 设置 onclick 属性

func SetOnFocus

func SetOnFocus(w AttrsMapper, script string)

SetOnFocus 设置 onfocus 属性

func SetOnFormChange

func SetOnFormChange(w AttrsMapper, script string)

SetOnFormChange 设置 onformchange 属性

func SetOnFormInput

func SetOnFormInput(w AttrsMapper, script string)

SetOnFormInput 设置 onforminput 属性

func SetOnInput

func SetOnInput(w AttrsMapper, script string)

SetOnInput 设置 oninput 属性

func SetOnInvalid

func SetOnInvalid(w AttrsMapper, script string)

SetOnInvalid 设置 oninvalid 属性

func SetOnKeyDown

func SetOnKeyDown(w AttrsMapper, script string)

SetOnKeyDown 设置 onkeydown 属性

func SetOnKeyPress

func SetOnKeyPress(w AttrsMapper, script string)

SetOnKeyPress 设置 onkeypress 属性

func SetOnKeyUp

func SetOnKeyUp(w AttrsMapper, script string)

SetOnKeyUp 设置 onkeyup 属性

func SetOnReset

func SetOnReset(w AttrsMapper, script string)

SetOnReset 设置 onreset 属性

func SetOnSelect

func SetOnSelect(w AttrsMapper, script string)

SetOnSelect 设置 onselect 属性

func SetOnSubmit

func SetOnSubmit(w AttrsMapper, script string)

SetOnSubmit 设置 onsubmit 属性

func SetReadOnly

func SetReadOnly(w AttrsMapper, readonly bool)

SetReadOnly 设置 readonly 属性

func SetRequired

func SetRequired(w AttrsMapper, required bool)

SetRequired 设置 required 属性

func SetSelected

func SetSelected(w AttrsMapper, checked bool)

SetSelected 设置 selected 属性

func SetSize

func SetSize(w AttrsMapper, size int)

SetSize 设置元素的 size

func SetSrc

func SetSrc(w AttrsMapper, src string)

SetSrc 设置 src 属性

func SetStep

func SetStep[N Number](w AttrsMapper, max N)

SetStep 设置 step 属性

func SetTarget

func SetTarget(w AttrsMapper, target string)

SetTarget 设置 target 属性

func SetTargetBlank

func SetTargetBlank(w AttrsMapper)

func SetTitle

func SetTitle(w AttrsMapper, title string)

SetTitle 设置 title 属性

func SetType

func SetType(w AttrsMapper, tp string)

SetType 设置 type 属性

func SetValue

func SetValue(w AttrsMapper, value string)

SetValue 设置 value 属性

func SetWidth

func SetWidth(w AttrsMapper, width string)

SetWidth 设置元素的 width

func StripTags

func StripTags(input string) string

func WalkParseFS

func WalkParseFS(t *template.Template, fsys fs.FS, root string, patterns ...string) (*template.Template, error)

WalkParseFS 遍历读取 fsys ,并将符合 pattern 的文件解析

pattern: 不能包含目录,有效值,如 *.html

注意:

  1. 所有 define 定义的块,全局应该不出现重名,在使用 template 方法渲染的时候,不应该添加其所在目录, 如在 user/index.html 文件中有 {{ define "status.part" }} Ok {{ end }}, 不管是在那个目录的那个文件,渲染该块,都不能添加目录: {{ template "status.part" .User }}
  2. 使用 template 渲染的时候,需要使用完整的路径,如 {{ template "user/index.html" . }}

func WithFuncMap

func WithFuncMap(tpl *template.Template) *template.Template

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 NewA

func NewA(href string) *Any

NewA 创建一个 <a>

func NewAny

func NewAny(tag string) *Any

NewAny 创建任意的 tag

func NewArticle

func NewArticle() *Any

NewArticle 创建一个 <article>

func NewBody

func NewBody() *Any

NewBody 创建一个 <body>

func NewButton

func NewButton(text string) *Any

func NewCode

func NewCode() *Any

NewCode 创建一个 <code>

func NewDL

func NewDL() *Any

NewDL 创建一个 <dl>

func NewDT

func NewDT() *Any

NewDT 创建一个 <dt>

func NewDatalist

func NewDatalist[T ~string](id string, ss []T) *Any

func NewDiv

func NewDiv() *Any

NewDiv 创建一个 <div>

func NewFieldSet

func NewFieldSet() *Any

NewFieldSet 创建一个 fieldset

func NewFigCaption

func NewFigCaption() *Any

NewFigCaption 创建一个 <figcaption>

func NewFigure

func NewFigure() *Any

NewFigure 创建一个 <figure>

func NewForm

func NewForm(method string, action string) *Any

NewForm 创建一个 form

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 NewHead

func NewHead() *Any

NewHead 创建一个 <head>

func NewInput

func NewInput(tp string, name string) *Any

NewInput 创建一个 input 标签

func NewInputButton

func NewInputButton(value string) *Any

NewInputButton 创建一个 button 标签

func NewInputCheckbox

func NewInputCheckbox(name string, value string, checked bool) *Any

func NewInputColor

func NewInputColor(name string) *Any

func NewInputData

func NewInputData(name string) *Any

func NewInputDateTimeLocal

func NewInputDateTimeLocal(name string) *Any

NewInputDateTimeLocal 选择时间(无时区),得到如 2024-10-21T13:07

func NewInputEmail

func NewInputEmail(name string) *Any

func NewInputMonth

func NewInputMonth(name string) *Any

NewInputMonth 选择月份和年份,使用浏览器的日期选择器,选择后得到如 2024-10

func NewInputNumber

func NewInputNumber(name string) *Any

func NewInputPassword

func NewInputPassword(name string) *Any

func NewInputRadio

func NewInputRadio(name string, value string, checked bool) *Any

func NewInputRange

func NewInputRange[N Number](name string, min N, max N, step N) *Any

func NewInputSearch

func NewInputSearch(name string) *Any

func NewInputSubmit

func NewInputSubmit(value string) *Any

NewInputSubmit 创建一个 submit 标签

func NewInputText

func NewInputText(name string) *Any

func NewInputTime

func NewInputTime(name string) *Any

NewInputTime 选择时间(无时区),得到如 12:02

func NewInputURL

func NewInputURL(name string) *Any

NewInputURL 包含 URL 地址的输入字段

func NewInputWeek

func NewInputWeek(name string) *Any

NewInputWeek 选择周和年

func NewLabel

func NewLabel(html string) *Any

func NewLegend

func NewLegend(e Element) *Any

NewLegend 创建一个 legend

func NewNav

func NewNav() *Any

NewNav 创建一个 <nav>

func NewOption

func NewOption(value string, b Element) *Any

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 NewP

func NewP() *Any

NewP 创建一个 <p>

func NewPre

func NewPre() *Any

NewPre 创建一个 <pre>

func NewScript

func NewScript() *Any

NewScript script 标签

func NewSelect

func NewSelect(name string, opts ...Element) *Any

NewSelect 创建一个 select

func NewStyle

func NewStyle() *Any

NewStyle style 标签

func NewTd

func NewTd(val ...Element) *Any

NewTd 创建一个新的 td

func NewTh

func NewTh(val ...Element) *Any

NewTh 创建一个新的 th

func NewTitle

func NewTitle(c Element) *Any

NewTitle 创建一个 <title>

func WithAddress

func WithAddress(es ...Element) *Any

func WithAny

func WithAny(a *Any, fn func(*Any)) *Any

WithAny 对 any 元素进行处理

func WithLabel

func WithLabel(es ...Element) *Any

WithLabel 使用 label 包裹元素

func (*Any) Add

func (c *Any) Add(children ...Element)

Add 添加子元素

func (*Any) HTML

func (c *Any) HTML() ([]byte, error)

HTML 实现 Element 接口

func (*Any) TplHTML

func (c *Any) TplHTML() template.HTML

type Attr

type Attr struct {
	// Key 属性的名字
	Key string

	// Values 属性值,可以有多个
	Values []string
}

Attr 一个属性

func (*Attr) Add

func (a *Attr) Add(value ...string)

Add 添加新的属性值

func (*Attr) Delete

func (a *Attr) Delete(value ...string)

Delete 删除属性值

func (*Attr) First

func (a *Attr) First() string

First 返回首个属性值

func (*Attr) Set

func (a *Attr) Set(value ...string)

Set 设置属性值

type Attrs

type Attrs struct {
	// Sep 多个属性间的连接符,当为空时,使用默认值 " " (一个空格)
	Sep string

	// KVSep key 和 value 之间的连接符,当为空时,使用默认值 =
	KVSep string
	// contains filtered or unexported fields
}

Attrs 多个属性

func (*Attrs) Attr

func (a *Attrs) Attr(key string) *Attr

Attr 返回一个指定的属性,若不存在,返回 nil

func (*Attrs) Delete

func (a *Attrs) Delete(keys ...string)

Delete 删除指定 key 的属性

func (*Attrs) GetKVSep

func (a *Attrs) GetKVSep() string

GetKVSep key 和 value 之间的连接符,当为空时, 返回默认值 =

func (*Attrs) GetSep

func (a *Attrs) GetSep() string

GetSep 多个属性间的连接符,当为空时,返回默认值 " " (一个空格)

func (*Attrs) HTML

func (a *Attrs) HTML() ([]byte, error)

HTML 转换为 HTML

func (*Attrs) Keys

func (a *Attrs) Keys() []string

Keys 返回所有属性的 key

func (*Attrs) MustAttr

func (a *Attrs) MustAttr(key string) *Attr

MustAttr 返回一个指定的属性,若不存在,返回 nil

func (*Attrs) Set

func (a *Attrs) Set(attr ...*Attr)

Set 设置属性值

type AttrsMapper

type AttrsMapper interface {
	MustAttrs() *Attrs
	FindAttrs() *Attrs
}

AttrsMapper 具有 AttrsMapper 方法

type Comment

type Comment string

Comment 注释

func (Comment) HTML

func (c Comment) HTML() ([]byte, error)

HTML 转换为 HTML

type Container

type Container interface {
	Add(children ...Element)
}

Container 允许添加子元素

type Element

type Element interface {
	HTML() ([]byte, error)
}

Element 所有 HTML 组件的基础定义

func ElementValue

func ElementValue(code []byte, err error) Element

type ElementFunc

type ElementFunc func() ([]byte, error)

func (ElementFunc) HTML

func (fn ElementFunc) HTML() ([]byte, error)

type Elements

type Elements []Element

Elements alias of []Element

func ToElements

func ToElements(es ...Element) Elements

ToElements 转换为 Elements 类型

func (Elements) HTML

func (hs Elements) HTML() ([]byte, error)

HTML 实现 Element 接口

type HTMLBytes

type HTMLBytes []byte

HTMLBytes 将 []byte 转换为 Element 类型,原样输出 HTML

func (HTMLBytes) Div

func (b HTMLBytes) Div() ([]byte, error)
Example
package main

import (
	"fmt"

	"github.com/xanygo/anygo/xhtml"
)

func main() {
	code := xhtml.HTMLBytes("hello")
	got, _ := code.Div()
	fmt.Println(string(got))

}
Output:

<div>hello</div>

func (HTMLBytes) HTML

func (b HTMLBytes) HTML() ([]byte, error)

HTML 实现 Element 接口

func (HTMLBytes) Li

func (b HTMLBytes) Li() ([]byte, error)

func (HTMLBytes) P

func (b HTMLBytes) P() ([]byte, error)

func (HTMLBytes) Pre

func (b HTMLBytes) Pre() ([]byte, error)

func (HTMLBytes) Wrap

func (b HTMLBytes) Wrap(tag string) ([]byte, error)

type HTMLString

type HTMLString string

HTMLString 将 String 转换为 Element 类型,原样输出 HTML

func (HTMLString) Div

func (b HTMLString) Div() ([]byte, error)

func (HTMLString) HTML

func (b HTMLString) HTML() ([]byte, error)

HTML 实现 Element 接口

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)

func (HTMLString) Wrap

func (b HTMLString) Wrap(tag string) ([]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 IMG

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

IMG 图片 img 标签

func NewIMG

func NewIMG(src string) *IMG

NewIMG 创建一个 img 标签

func (*IMG) ALT

func (m *IMG) ALT(alt string) *IMG

ALT 设置 alt 属性

func (*IMG) HTML

func (m *IMG) HTML() ([]byte, error)

HTML 转换为 html

func (*IMG) SRC

func (m *IMG) SRC(src string) *IMG

SRC 设置 src 属性

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

Link 页面元素 link 标签

func NewLink() *Link

NewLink 创建一个新的 link 标签

func (*Link) HTML

func (a *Link) HTML() ([]byte, error)

HTML 转换为 html

func (*Link) Href

func (a *Link) Href(href string) *Link

Href 设置 href 属性

func (*Link) Rel

func (a *Link) Rel(rel string) *Link

Rel 设置 rel 属性

func (*Link) Type

func (a *Link) Type(tp string) *Link

Type 设置 tp 属性

type Meta

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

Meta 页面元信息标签 meth

func NewMeta

func NewMeta() *Meta

NewMeta 创建一个新的 <meta>

func (*Meta) Charset

func (a *Meta) Charset(charset string) *Meta

Charset 设置 charset 属性

func (*Meta) Content

func (a *Meta) Content(content string) *Meta

Content 设置 content 属性

func (*Meta) HTML

func (a *Meta) HTML() ([]byte, error)

HTML 转换为 html

func (*Meta) HTTPEquiv

func (a *Meta) HTTPEquiv(equiv string) *Meta

HTTPEquiv 设置 http-equiv 属性

func (*Meta) Media

func (a *Meta) Media(media string) *Meta

Media 设置 media 属性

func (*Meta) Name

func (a *Meta) Name(name string) *Meta

Name 设置 name 属性

type Number

type Number interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 |
		~float32 | ~float64
}

type Pre

type Pre string

Pre 输出 HTML 时添加 pre 标签

func (Pre) HTML

func (p Pre) HTML() ([]byte, error)

type PreByte

type PreByte []byte

PreByte 输出 HTML 时添加 pre 标签

func (PreByte) HTML

func (p PreByte) HTML() ([]byte, error)

type StyleAttr

type StyleAttr struct {
	WithAttrs
}

StyleAttr style 属性

func (*StyleAttr) BackgroundColor

func (s *StyleAttr) BackgroundColor(color string) *StyleAttr

BackgroundColor 设置背景演示

func (*StyleAttr) Border

func (s *StyleAttr) Border(val string) *StyleAttr

Border 设置边框属性

func (*StyleAttr) Color

func (s *StyleAttr) Color(color string) *StyleAttr

Color 设置前景/字体颜色

func (*StyleAttr) Font

func (s *StyleAttr) Font(val string) *StyleAttr

Font 设置字体

func (*StyleAttr) FontFamily

func (s *StyleAttr) FontFamily(val string) *StyleAttr

FontFamily 设置字体系列(字体族)

func (*StyleAttr) FontSize

func (s *StyleAttr) FontSize(val string) *StyleAttr

FontSize 设置字体大小

func (*StyleAttr) FontWeight

func (s *StyleAttr) FontWeight(val string) *StyleAttr

FontWeight 设置字体粗细

func (*StyleAttr) Get

func (s *StyleAttr) Get(key string) string

Get 获取属性值

func (*StyleAttr) HTML

func (s *StyleAttr) HTML() ([]byte, error)

HTML 实现 Element 接口

func (*StyleAttr) Height

func (s *StyleAttr) Height(h string) *StyleAttr

Height 设置高度

func (*StyleAttr) Margin

func (s *StyleAttr) Margin(val string) *StyleAttr

Margin 设置外边距

func (*StyleAttr) MaxHeight

func (s *StyleAttr) MaxHeight(h string) *StyleAttr

MaxHeight 设置最大高度

func (*StyleAttr) MaxWidth

func (s *StyleAttr) MaxWidth(w string) *StyleAttr

MaxWidth 设置最大新宽度

func (*StyleAttr) MinHeight

func (s *StyleAttr) MinHeight(h string) *StyleAttr

MinHeight 设置最小高度

func (*StyleAttr) MinWidth

func (s *StyleAttr) MinWidth(w string) *StyleAttr

MinWidth 设置最小宽度

func (*StyleAttr) Padding

func (s *StyleAttr) Padding(val string) *StyleAttr

Padding 设置内边距

func (*StyleAttr) SetTo

func (s *StyleAttr) SetTo(a AttrsMapper) error

SetTo 将样式信息设置到指定的属性集合

func (*StyleAttr) TextAlign

func (s *StyleAttr) TextAlign(align string) *StyleAttr

TextAlign 设置内容对齐方式

func (*StyleAttr) Width

func (s *StyleAttr) Width(w string) *StyleAttr

Width 设置宽度

type TPLRequest

type TPLRequest struct {
	Request *http.Request
	// contains filtered or unexported fields
}

func NewTPLRequest

func NewTPLRequest(req *http.Request) *TPLRequest

func (*TPLRequest) Context

func (t *TPLRequest) Context() context.Context

func (*TPLRequest) Dir

func (t *TPLRequest) Dir() string

Dir 当前地址的父目录

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) Query

func (t *TPLRequest) Query(field string) string

Query 获取 url 的 query 参数值

func (*TPLRequest) QueryEQ

func (t *TPLRequest) QueryEQ(queryPair ...any) (bool, error)

QueryEQ 判断 Query 参数是否相等 参数 queryPair 必须是双数,依次为: 字段名,字段值,字段名,字段值 字段名必须是 string 类型

func (*TPLRequest) QueryIn

func (t *TPLRequest) QueryIn(field string, values []string) bool

func (*TPLRequest) WithNewQuery

func (t *TPLRequest) WithNewQuery(queryPair ...any) (template.URL, error)

WithNewQuery 基于当前 url 的 Path,生成新的链接

queryPair:url 中的 query 参数,必须成对出现,如 "a",1,"b","2","c","" 值为空的会忽略

func (*TPLRequest) WithQuery

func (t *TPLRequest) WithQuery(queryPair ...any) (template.URL, error)

WithQuery 基于当前 url,生成新的链接

queryPair:url 中的 query 参数,必须成对出现,如 "a",1,"b","2","c","" 同名参数会将当前链接中的同名参数覆盖,值为空的则将其删除

type Table1

type Table1 struct {
	WithAttrs
	// contains filtered or unexported fields
}

Table1 一个简单的表格

func (*Table1) AddRow

func (t *Table1) AddRow(cells ...Element)

AddRow 添加一行内容

func (*Table1) AddRows

func (t *Table1) AddRows(rows ...[]Element)

AddRows 添加多行内容

func (*Table1) HTML

func (t *Table1) HTML() ([]byte, error)

HTML 实现 Element 接口

func (*Table1) SetFooter

func (t *Table1) SetFooter(cells ...Element)

SetFooter 设置表格的页脚

func (*Table1) SetHeader

func (t *Table1) SetHeader(cells ...Element)

SetHeader 设置表头

type TextBytes

type TextBytes []byte

TextBytes 将 []byte 转换为 Element 类型,会转换为 html.EscapeString

func (TextBytes) Div

func (b TextBytes) Div() ([]byte, error)

func (TextBytes) HTML

func (b TextBytes) HTML() ([]byte, error)

HTML 实现 Element 接口

func (TextBytes) Li

func (b TextBytes) Li() ([]byte, error)

func (TextBytes) P

func (b TextBytes) P() ([]byte, error)

func (TextBytes) Pre

func (b TextBytes) Pre() ([]byte, error)

func (TextBytes) Wrap

func (b TextBytes) Wrap(tag string) ([]byte, error)

type TextString

type TextString string

TextString 文本,输出的时候会自动调用 html.EscapeString

func (TextString) Div

func (b TextString) Div() ([]byte, error)

func (TextString) HTML

func (b TextString) HTML() ([]byte, error)

HTML 实现 Element 接口

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)

func (TextString) Wrap

func (b TextString) Wrap(tag string) ([]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>

type WithAttrs

type WithAttrs struct {
	Attrs *Attrs
}

WithAttrs 具有 attrs 属性

func (*WithAttrs) FindAttrs

func (w *WithAttrs) FindAttrs() *Attrs

FindAttrs 返回当前的 Attrs

func (*WithAttrs) MustAttrs

func (w *WithAttrs) MustAttrs() *Attrs

MustAttrs 若 attrs 不存在,则创建 并返回 attrs

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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