gohtml

package module
v0.0.0-...-524493d Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2020 License: MIT Imports: 3 Imported by: 0

README

gohtml

一个使用go动态生成html的包,通常用于后端渲染html。

[toc]

安装教程

go get gitee.com/Klarkxy/gohtml

使用说明

第一个例子
htm1 := gohtml.NewTag()
htm1.Tag("p").Text("Hello World!")
fmt.Println(htm1.String())

输出

<p >Hello World! </p>

Hello World!

加入一些属性
htm2 := gohtml.NewTag()
htm2.Tag("a", gohtml.P{"href": "www.baidu.com"}).Text("前往百度")
fmt.Println(htm2.String())

输出

<a href="www.baidu.com" >前往百度 </a>

前往百度

其中gohtml.P=map[string]string,可以通过导入map来导入多个属性

嵌套
htm3 := gohtml.NewTag()
htm3.Tag("table", gohtml.P{"border": "1"}).Func(func(Htm *gohtml.GoTag) {
	Htm.Tag("tr").Tag("td").Text("1").
		Master().Tag("td").Text("2")
	Htm.Tag("tr").Tag("td").Text("3").
		Master().Tag("td").Text("4")
	})

输出

<table border="1" >
<tr >
<td >1</td>
<td >2</td>
</tr>
<tr >
<td >3</td>
<td >4</td>
</tr>
</table>
1 2
3 4

该例中使用了两种嵌套方式,一种是使用Func(func(*gohtml.GoTag)函数进行嵌套,另一种是使用Master()函数返回上一层

快捷语义
  • T(name string) 同Tag(name),插入一个Tag,返回这个Tag
  • S(name) 插入一个Self-Closing Tag
  • P(param, value string) 往当前Tag中加入一条属性param="value"

一般通常情况下建议使用这种模式

htm4 := gohtml.NewTag()
form := htm4.T("form")
form.S("input").P("type", "text").P("placeholder", "用户名")
form.S("input").P("type", "password").P("placeholder", "密码")
form.T("button").P("type", "submit").Text("登录")
fmt.Println(htm4.String())

输出

<form >
<input placeholder="用户名" type="text" >
<input type="password" placeholder="密码" >
<button type="submit" >登录</button>
</form>

同时,S()T()中也可以直接成对插入属性

	htm6 := gohtml.NewTag()
	htm6.S("input", "type", "text", "placeholder", "Hello World!")
	htm6.T("button", "type", "submit").Text("登录")
	fmt.Println(htm6.String())

输出

<input type="text" placeholder="Hello World!" >
<button type="submit" >登录</button>
IfP语义

根据条件设置属性

  • IfP(flag, A, B) ==> if (flag) A="B" else nil
  • IfP(flag, A, B, C, D) ==> if (flag) A="B" else C="D"
	htm7 := gohtml.NewTag()
	canedit := false
	htm7.S("input", "type", "text").IfP(canedit, "readonly", "readonly")
	canedit = true
	htm7.S("input", "type", "text").IfP(canedit, "readonly", "readonly")
	ispassword := false
	htm7.S("input").IfP(ispassword, "type", "password", "type", "text")
	ispassword = true
	htm7.S("input").IfP(ispassword, "type", "password", "type", "text")
	fmt.Println(htm7.String())

输出

<input type="text" >
<input type="text" readonly="readonly" >
<input type="text" >
<input type="password" >
Html模板

使用tag.Html(name, filepath)来导入一个指定位置的Html 如果要使用模板,则使用tag.Html(name, filepath).Excute(data)来导入模板。模板使用方式详见html/template

htm5 := gohtml.NewTag()
PWD, _ := os.Getwd()
htm5.Html("index.html", PWD+"/index.html").Execute("Hello World!")
fmt.Println(htm5.String())

联系我

如果您在使用时遇见了问题,或者有什么意见或者建议,请务必联系我,谢谢。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GoHtm

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

func (*GoHtm) Execute

func (cur *GoHtm) Execute(data interface{}) *GoHtm

设置模板参数。 模板用法详见html/template

func (*GoHtm) Master

func (cur *GoHtm) Master() *GoTag

func (*GoHtm) String

func (cur *GoHtm) String() string

type GoTag

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

GoTag: <{name} {property}>{tags}</{name}>

func NewTag

func NewTag() *GoTag

创建一个GoTag对象

func Tag

func Tag(name string, propertys ...P) *GoTag

func (*GoTag) Append

func (cur *GoTag) Append(txts ...fmt.Stringer) *GoTag

在当前Tag中插入若干个Tag/Text,返回值为当前Tag 谨慎调用,可能会出现重复的情况

func (*GoTag) Func

func (cur *GoTag) Func(fn func(*GoTag))

更复杂的嵌套,用函数来实现吧 该函数没有返回值,通常作为一条语句的最末端,需要返回值用FuncR

func (*GoTag) FuncR

func (cur *GoTag) FuncR(fn func(*GoTag) *GoTag) *GoTag

更复杂的嵌套,用函数来实现吧 该函数带返回值,返回值为当前Tag

func (*GoTag) Html

func (cur *GoTag) Html(name, filepath string) *GoHtm

在当前Tag里新建一个Html模板,返回值为新建出来的模板

func (*GoTag) IfP

func (cur *GoTag) IfP(flag bool, properties ...string) *GoTag

if表达式,根据条件判断设置属性 IfP(flag, A, B) ==> if (flag) A="B" else nil IfP(flag, A, B, C, D) ==> if (flag) A="B" else C="D"

func (*GoTag) Master

func (cur *GoTag) Master() *GoTag

func (*GoTag) P

func (cur *GoTag) P(name, value string) *GoTag

设置属性 name="value",返回值为当前GoTag 一次设置一组,如有重复则覆盖

func (*GoTag) PS

func (cur *GoTag) PS(name string) *GoTag

设置属性 name="name",返回值为当前GoTag 一次设置一组,如有重复则覆盖

func (*GoTag) S

func (cur *GoTag) S(name string, properties ...string) *GoTag

往cur中插入一个Self-Closing Tag,返回值为插入的这个Tag 可以直接一次性导入多个属性,按顺序分为一对一对

func (*GoTag) SelfClosing

func (cur *GoTag) SelfClosing() *GoTag

设置当前Tag为self-Closing,返回当前Tag

func (*GoTag) String

func (cur *GoTag) String() string

func (*GoTag) T

func (cur *GoTag) T(name string, properties ...string) *GoTag

往cur中插入一个Tag,返回值为插入的这个Tag 可以直接一次性导入多个属性,按顺序分为一对一对

func (*GoTag) Tag

func (cur *GoTag) Tag(name string, propertys ...P) *GoTag

在当前Tag下新建一个Tag,返回值为新建出来的Tag

func (*GoTag) Text

func (cur *GoTag) Text(str string) *GoTag

在当前Tag中插入一条string

type P

type P = map[string]string

作为参数使用,能少写很多字符

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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