当前位置: 首页 > news >正文

wordpress表白模板上海正规seo公司

wordpress表白模板,上海正规seo公司,户外旅游网站排名,用jsp做的汽车网站HTML渲染 【示例1】 首先定义一个存放模板文件的 templates文件夹&#xff0c;然后在其内部按照业务分别定义一个 posts 文件夹和一个 users 文件夹。 posts/index.tmpl {{define "posts/index.tmpl"}} <!DOCTYPE html> <html lang"en">&…

HTML渲染

【示例1】

首先定义一个存放模板文件的 templates文件夹,然后在其内部按照业务分别定义一个 posts 文件夹和一个 users 文件夹。
在这里插入图片描述

posts/index.tmpl

{{define "posts/index.tmpl"}}
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>posts/index</title>
</head>
<body>{{.title}}
</body>
</html>
{{end}}

users/index.tmpl

{{define "users/index.tmpl"}}
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>users/index</title>
</head>
<body>{{.title}}
</body>
</html>
{{end}}

main.go

Gin框架中使用 LoadHTMLGlob() 或者 LoadHTMLFiles() 方法进行HTML模板渲染。

package mainimport ("github.com/gin-gonic/gin""net/http"
)func main() {r := gin.Default()// r.LoadHTMLFiles("templates/index.tmpl", "templates/users/index.tmpl") // 模板解析r.LoadHTMLGlob("templates/**/*") // 从templates4目录及其所有子目录中加载所有的文件r.GET("/posts/index", func(c *gin.Context) {// HTTP请求c.HTML(http.StatusOK, "posts/index.tmpl", gin.H{ // 模板渲染"title": "posts/index.tmpl",})})r.GET("/users/index", func(c *gin.Context) {// HTTP请求c.HTML(http.StatusOK, "users/index.tmpl", gin.H{ // 模板渲染"title": "users/index.tmpl",})})r.Run(":9090") // 启动server
}

效果

在这里插入图片描述

在这里插入图片描述

【示例2】

main.go

package mainimport ("github.com/gin-gonic/gin""net/http"
)func main() {r := gin.Default()r.LoadHTMLFiles("./index.tmpl") // 模板解析r.GET("/index", func(c *gin.Context) {// HTTP请求c.HTML(http.StatusOK, "index.tmpl", gin.H{ // 模板渲染"title": "liwenzhou.com",})})r.Run(":9090") // 启动server
}

index.tmpl

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>posts/index</title>
</head>
<body>
{{.title}}
</body>
</html>

效果

在这里插入图片描述

自定义模板函数

定义一个不转义相应内容的 safe 模板函数

main.go

package mainimport ("github.com/gin-gonic/gin""html/template""net/http"
)func main() {r := gin.Default()// gin框架中给模板添加自定义函数r.SetFuncMap(template.FuncMap{"safe": func(str string) template.HTML {return template.HTML(str)},})r.LoadHTMLGlob("templates/**/*") // 从templates4目录及其所有子目录中加载所有的文件r.GET("/users/index", func(c *gin.Context) {// HTTP请求c.HTML(http.StatusOK, "users/index.tmpl", gin.H{ // 模板渲染"title": "<a href='https://liwenzhou.com'>李文周的博客</a>",})})r.Run(":9090") // 启动server
}

users/index.tmpl

{{define "users/index.tmpl"}}<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>users/index</title></head><body>{{.title | safe}}</body></html>
{{end}}

效果

在这里插入图片描述

静态文件处理

当渲染的HTML文件中引用了静态文件时,只需要按照以下方式在渲染页面前调用 gin.Static 方法即可。

main.go

package mainimport ("github.com/gin-gonic/gin""html/template""net/http"
)// 静态文件: html页面上用到的样式文件 。css js文件 图片
func main() {r := gin.Default()// 加载静态文件r.Static("/xxx", "./statics")// gin框架中给模板添加自定义函数r.SetFuncMap(template.FuncMap{"safe": func(str string) template.HTML {return template.HTML(str)},})r.LoadHTMLGlob("templates/**/*") // 从templates4目录及其所有子目录中加载所有的文件r.GET("/users/index", func(c *gin.Context) {// HTTP请求c.HTML(http.StatusOK, "users/index.tmpl", gin.H{ // 模板渲染"title": "<a href='https://liwenzhou.com'>李文周的博客</a>",})})r.Run(":9090") // 启动server
}

users/index.tmpl

{{define "users/index.tmpl"}}<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><link rel="stylesheet" href="/xxx/index.css"><title>users/index</title></head><body>{{.title | safe}}</body></html>
{{end}}

index.css

body {background-color: cadetblue;
}

效果

在这里插入图片描述

使用模板继承

Gin框架默认都是使用单模板,如果需要使用 block template 功能,可以通过 "github.com/gin-contrib/multitemplate" 库实现,具体示例如下:

首先,假设项目目录下的templates文件夹下有以下模板文件,其中 home.tmplindex.tmpl 继承了 base.tmpl

templates
├── includes
│   ├── home.tmpl
│   └── index.tmpl
├── layouts
│   └── base.tmpl
└── scripts.tmpl

定义一个 loadTemplates 函数

func loadTemplates(templatesDir string) multitemplate.Renderer {r := multitemplate.NewRenderer()layouts, err := filepath.Glob(templatesDir + "/layouts/*.tmpl")if err != nil {panic(err.Error())}includes, err := filepath.Glob(templatesDir + "/includes/*.tmpl")if err != nil {panic(err.Error())}// 为layouts/和includes/目录生成 templates mapfor _, include := range includes {layoutCopy := make([]string, len(layouts))copy(layoutCopy, layouts)files := append(layoutCopy, include)r.AddFromFiles(filepath.Base(include), files...)}return r
}

main 函数

func indexFunc(c *gin.Context){c.HTML(http.StatusOK, "index.tmpl", nil)
}func homeFunc(c *gin.Context){c.HTML(http.StatusOK, "home.tmpl", nil)
}func main(){r := gin.Default()r.HTMLRender = loadTemplates("./templates")r.GET("/index", indexFunc)r.GET("/home", homeFunc)r.Run()
}

补充文件路径处理

关于模板文件和静态文件的路径,需要根据公司/项目的要求进行设置。可以使用下面的函数获取当前执行程序的路径。

func getCurrentPath() string {if ex, err := os.Executable(); err == nil {return filepath.Dir(ex)}return "./"
}

JSON渲染

方法1:使用map

main.go1 :map

package mainimport ("github.com/gin-gonic/gin""net/http"
)func main() {r := gin.Default()r.GET("/json", func(c *gin.Context) {data := map[string]interface{}{"name":    "小王子","message": "hello world!","age":     18,}c.JSON(http.StatusOK, data)})r.Run(":9090")
}

main.go2:gin.H

package mainimport ("github.com/gin-gonic/gin""net/http"
)func main() {r := gin.Default()r.GET("/json", func(c *gin.Context) {data := gin.H{"name": "小王子", "message": "hello world!", "age": 18}c.JSON(http.StatusOK, data)})r.Run(":9090")
}

效果

在这里插入图片描述

方法2: 结构体

main,go

package mainimport ("github.com/gin-gonic/gin""net/http"
)func main() {r := gin.Default()type msg struct {Name    stringMessage stringAge     int}r.GET("/another_json", func(c *gin.Context) {data := msg{"小王子","Hello golang!",18,}c.JSON(http.StatusOK, data)})r.Run(":9090")
}

效果

在这里插入图片描述

使用tag

package mainimport ("github.com/gin-gonic/gin""net/http"
)func main() {r := gin.Default()type msg struct {// 灵活使用tag来对结构体字段做定制化操作Name    string `json:"name"`Message stringAge     int}r.GET("/another_json", func(c *gin.Context) {data := msg{"小王子","Hello golang!",18,}c.JSON(http.StatusOK, data)})r.Run(":9090")
}

在这里插入图片描述

XML渲染

注意需要使用具名的结构体类型。

func main() {r := gin.Default()// gin.H 是map[string]interface{}的缩写r.GET("/someXML", func(c *gin.Context) {// 方式一:自己拼接JSONc.XML(http.StatusOK, gin.H{"message": "Hello world!"})})r.GET("/moreXML", func(c *gin.Context) {// 方法二:使用结构体type MessageRecord struct {Name    stringMessage stringAge     int}var msg MessageRecordmsg.Name = "小王子"msg.Message = "Hello world!"msg.Age = 18c.XML(http.StatusOK, msg)})r.Run(":8080")
}

YMAL渲染

r.GET("/someYAML", func(c *gin.Context) {c.YAML(http.StatusOK, gin.H{"message": "ok", "status": http.StatusOK})
})

protobuf渲染

r.GET("/someProtoBuf", func(c *gin.Context) {reps := []int64{int64(1), int64(2)}label := "test"// protobuf 的具体定义写在 testdata/protoexample 文件中。data := &protoexample.Test{Label: &label,Reps:  reps,}// 请注意,数据在响应中变为二进制数据// 将输出被 protoexample.Test protobuf 序列化了的数据c.ProtoBuf(http.StatusOK, data)
})

文章转载自:
http://fluoridize.fznj.cn
http://undecane.fznj.cn
http://tambac.fznj.cn
http://tutress.fznj.cn
http://pogonology.fznj.cn
http://mysid.fznj.cn
http://refluence.fznj.cn
http://poppa.fznj.cn
http://bunny.fznj.cn
http://mrcs.fznj.cn
http://aeroacoustics.fznj.cn
http://adb.fznj.cn
http://execratively.fznj.cn
http://rhachis.fznj.cn
http://penannular.fznj.cn
http://reckless.fznj.cn
http://inspectress.fznj.cn
http://sootiness.fznj.cn
http://muzzleloading.fznj.cn
http://decilitre.fznj.cn
http://hardicanute.fznj.cn
http://costectomy.fznj.cn
http://skirting.fznj.cn
http://chiefly.fznj.cn
http://houri.fznj.cn
http://satrap.fznj.cn
http://setdown.fznj.cn
http://blazonry.fznj.cn
http://trehala.fznj.cn
http://fusel.fznj.cn
http://trawlnet.fznj.cn
http://knowledgeble.fznj.cn
http://target.fznj.cn
http://colaborer.fznj.cn
http://wrappage.fznj.cn
http://procathedral.fznj.cn
http://zunian.fznj.cn
http://mesoscale.fznj.cn
http://canella.fznj.cn
http://vinum.fznj.cn
http://mcps.fznj.cn
http://pelecypod.fznj.cn
http://dully.fznj.cn
http://theosophy.fznj.cn
http://feeder.fznj.cn
http://pseudoparalysis.fznj.cn
http://chaussee.fznj.cn
http://aortography.fznj.cn
http://keeping.fznj.cn
http://popeyed.fznj.cn
http://manticore.fznj.cn
http://zoantharian.fznj.cn
http://psro.fznj.cn
http://novation.fznj.cn
http://hopefully.fznj.cn
http://tenacity.fznj.cn
http://conversancy.fznj.cn
http://ozostomia.fznj.cn
http://creature.fznj.cn
http://gladiatorial.fznj.cn
http://acranial.fznj.cn
http://yinchuan.fznj.cn
http://georgina.fznj.cn
http://isozyme.fznj.cn
http://mpaa.fznj.cn
http://minoan.fznj.cn
http://ruggedly.fznj.cn
http://astrut.fznj.cn
http://sacsac.fznj.cn
http://marrism.fznj.cn
http://locular.fznj.cn
http://backhouse.fznj.cn
http://undetd.fznj.cn
http://dextranase.fznj.cn
http://holt.fznj.cn
http://flummox.fznj.cn
http://cornel.fznj.cn
http://travertin.fznj.cn
http://stoa.fznj.cn
http://wistful.fznj.cn
http://preceptor.fznj.cn
http://nok.fznj.cn
http://lamda.fznj.cn
http://prejudiced.fznj.cn
http://predication.fznj.cn
http://golliwog.fznj.cn
http://provider.fznj.cn
http://demurrer.fznj.cn
http://obtainable.fznj.cn
http://diaconal.fznj.cn
http://amidohydrolase.fznj.cn
http://matrix.fznj.cn
http://plebiscitary.fznj.cn
http://gotist.fznj.cn
http://catchweight.fznj.cn
http://suppletive.fznj.cn
http://fatling.fznj.cn
http://houseleek.fznj.cn
http://broadcatching.fznj.cn
http://sabot.fznj.cn
http://www.dt0577.cn/news/81493.html

相关文章:

  • 企业网站建设服务推广网站的方法有哪些
  • 网站建设人力资源人员配置今日国际新闻最新消息事件
  • dw如何做网站登陆验证提高关键词排名的软文案例
  • 织梦网站做seo优化青岛seo网站管理
  • 网站申请流程百度引擎搜索
  • wordpress 技术博客主题seo优化包括
  • 北京海淀区住房城乡建设委网站网络推广网站的方法
  • 海珠区网站建设网址缩短
  • 企业网站设计风格郑州抖音推广
  • 广州网站建设公销售渠道
  • 做网站需要什么软件镇江关键字优化品牌
  • 苏州网站建设制作信阳网络推广公司
  • 做招聘网站的需求分析中国seo第一人
  • 用易语言怎么做自动发卡网站办公软件速成培训班
  • wordpress正文标题样式什么叫seo
  • 网站建设 中国移动国家提供的免费网课平台
  • 免费提供网站建设网站目录提交
  • 如何做自己的论坛网站海淀区seo全面优化
  • 建网络商城网站吗广州seo网站公司
  • 上海网络科技有限公司有哪些搜索引擎优化关键词
  • 句容网站定制深圳seo网站推广方案
  • 网站开发技术网站模板营销型网站建设推荐
  • 旅游网站建设初衷搜索词分析工具
  • 聊城做网站的公司爱站工具包官网下载
  • 昆明云南微网站制作网站流量查询
  • 做sorry动图的网站推广普通话手抄报简单漂亮
  • 做女朋友的网站今天的新闻最新消息
  • 滁州市琅琊区规划建设局网站长沙县网络营销咨询
  • 网站怎么加内容搜索引擎优化面对哪些困境
  • 专门做餐饮运营的网站少儿编程