建设网站5要素,热门的网站模板,国家信息公示系统官网,网络营销策划的基本策略go模板语句
以下是一些go语言模板引擎的一些简单知识和使用
基础语法
重要#xff01;#xff01;#xff01;#xff1a;
模板在写动态页面的网站的时候#xff0c;我们常常将不变的部分提出成为模板#xff0c;可变部分通过后端程序的渲染来生成动态网页#xff0…go模板语句
以下是一些go语言模板引擎的一些简单知识和使用
基础语法
重要
模板在写动态页面的网站的时候我们常常将不变的部分提出成为模板可变部分通过后端程序的渲染来生成动态网页 golang也支持模板渲染。 【1】模板内内嵌的语法支持全部需要加 {{}} 来标记。
【2】在模板文件内 . 代表了当前位置的上下文 1在非循环体内. 就代表了后端传过来的上下文 2在循环体中. 就代表了循环的上下文访问当前迭代的元素意思是切片的话循环里就是指数组的单个元素 【3】在模板文件内 $.代表了模板根级的上下文 【4】在模板文件内$有两种用法第一种代表了当前迭代的索引值 例子 {{ range $index, $element : .Items }}Index: {{ $index }}, Element: {{ $element }}
{{ end }}第二种在模板中定义和赋值变量这个之后会讲到 {{ $variable : .SomeValue }}
后续的模板代码中使用 $variable来表示.SomeValue简单示例
前端
!DOCTYPE html
html langen
headmeta charsetUTF-8titleGin模板引擎示例/title
/head
bodyh1Hello, {{.Name}}!/h1
/body
/html后端
{{.Name}}实际就是获取了map中name字段的值package mainimport (github.com/gin-gonic/gin
)func main() {r : gin.Default()// 加载模板文件r.LoadHTMLGlob(templates/*)// 定义路由r.GET(/hello, func(c *gin.Context) {// 渲染模板并传递数据c.HTML(200, index.html, gin.H{Name: Gin User,//注意gin.H{title: Hello Gin}实际是map[string]interface{}通过用{{.Name}}实际就是获取了map中name字段的值.就是上下文中gin.H传过来的数据})})// 启动应用r.Run(:8080)
}
对符号的支持 1. 字符串{{ “abc啦啦啦”}} 输出 abc啦啦啦
2. 原始字符串用转义字符{ {啦啦啦 } } { { a } } 不会转义 输出 啦啦啦a
3. 字节类型{ { a } } --- 97 会转义 输出 97
4. 打印打印 :字符串 { { print abc李总辉 } } 输出abc李总辉nil类型{ { print nil } } 输出nil变量的定义和使用
演示
讲到了$的第二个作用变量的定义
!DOCTYPE html
html langen
headmeta charsetUTF-8titleTitle/title
/head
body变量的定义{{$name : 马士兵888}}变量的使用{{$name}}
/body
/html
//最后会输出 李总辉888模板语法
if语句:
支持最简单的bool类型和字符串类型的判断
当.condition为bool类型的时候则为true表示执行当.condition为string类型的时候则非空表示执行。
{{if .Condition}}{{/* 如果条件为真执行此处的内容 */}}
{{else}}{{/* 如果条件为假执行此处的内容 */}}
{{end}}循环语句
刚刚最开始也演示过
{{range .Items}}{{.}} {{/* 输出当前迭代的元素 */}}
{{end}}内置的模板函数
用到的时候再去看吧
not 非
and 与
or 或
eq 等于
ne 不等于
lt 小于 (less than)
le 小于等于
gt 大于
ge 大于等于with的使用
其实就是提供的一个简便的写法用于创建一个新的局部作用域
使用起来很简单
{{with .Value}}{{/* 在这个作用域内可以直接访问 .Value 就是说把.Value变成了.来操作*/}}
{{end}}比如
原写法
Name: {{.Person.Name}}
Age: {{.Person.Age}}使用with
{{with .Person}}Name: {{.Name}}Age: {{.Age}}
{{end}}模板函数
【1】print 打印字符串
【2】printf 按照格式化的字符串输出
格式参照Go中fmt.Sprintf
PS : 具体的格式参照GO中格式https://studygolang.com/pkgdoc
【3】len 返回对应类型的长度(map, slice, array, string, chan)
【4】管道传递符 |
函数中使用管道传递过来的数值
【5】括号提高优先级别()进阶示例
后端
func Hello1(context *gin.Context){//定义数据age : 19arr : []int{33,66,99}//将age 和arr放入map中map_data : map[string]interface{}{age : age,arr : arr,}//获取路径中的参数值context.HTML(200,demo01/hello.html,map_data)将定义的数据age和arr传递给HTML模板然后使用Gin框架将渲染后的HTML作为HTTP响应发送给客户端。在hello.html模板中使用age和arr的值来动态生成页面内容。前端
body{{.age}
{{range .arr}}{{.}}{{$.age}}
{{end}}
br
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
{{/*$i,$v 从 后端传过来的map_data这个上下文中获取数据 */}}
{{range $i,$v : .arr}}{{$i}}{{$v}}
{{end}}/body后端
body{{/*获取后端传过来的map_data中的内容*/}}{{/* . 当前的上下文后端传过来的map_data这个上下文*/}}{{.age}}{{/*. 当前的上下文后端传过来的map_data这个上下文*/}}{{range .arr}}{{/*. 上下文指的就是.arr这个上下文指的就是遍历的每一个元素*/}}{{.}}{{/*在循环内部想获取根级上下文中的age的话就需要使用$.来获取 map_data这个上下文*/}}{{$.age}}{{end}}br、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、{{/*$i,$v 从 后端传过来的map_data这个上下文中获取数据 */}}{{range $i,$v : .arr}}{{$i}}{{$v}}{{end}}
/body模版嵌套
为什么会有嵌套呢他的作用其实还是很大的主要用于在基础模版上充当更改部分模板的功能
示例
后端
// main.go
package mainimport (html/templatenet/httpgithub.com/gin-gonic/gin
)var templates *template.Templatefunc init() {// 解析所有模板文件templates template.Must(template.ParseGlob(templates/*.html))
}func main() {router : gin.Default()// 基础路由router.GET(/, func(c *gin.Context) {// 渲染 index.html不需要嵌套其他模板renderTemplate(c, index.html, gin.H{Title: Home, Content: Welcome to the home page})})// 带有模板嵌套的路由router.GET(/about, func(c *gin.Context) {// 渲染 base.html并在 content 块中嵌套 about.htmlrenderTemplate(c, base.html, gin.H{Title: About, Content: about.html})})router.Run(:8080)
}func renderTemplate(c *gin.Context, tmpl string, data gin.H) {// 执行模板渲染err : templates.ExecuteTemplate(c.Writer, tmpl, data)if err ! nil {// 处理错误c.AbortWithError(http.StatusInternalServerError, err)}
}// 路由渲染的是 index.html 模板而 /about 路由渲染的是 base.html 模板并在 content 块中嵌套了 about.html 模板。前端三个文件
------------templates/base.html-----------
!DOCTYPE html
html langen
headmeta charsetUTF-8title{{.Title}}/title
/head
bodydiv idcontent{{ template content . }}/div
/body
/html-------------- templates/index.html--------------
{{ define content }}h1{{.Title}}/h1p{{.Content}}/p
{{ end }}---------templates/about.html------------
{{ define content }}h1{{.Title}}/h1pThis is the content of the about page./p
{{ end }}
模板文件之间通过 {{ template content . }} 来实现了模板嵌套。
基础模板 “base.html” 定义了一个 content 块子模板通过定义同名的块来覆盖或扩展基础模板中的内容。
啥意思呢其实就是**base.html 充当了一个基础布局模板而 index.html 和 about.html 分别充当了具体页面的内容模板。通过模板嵌套可以轻松地在不同页面之间共享基础布局提高模板的复用性。**
模版继承
模板继承是一种模板组织方式通过它你可以创建一个包含通用结构和样式的基础模板并在此基础上创建具体内容的子模板。子模板可以覆盖或扩展基础模板中的块从而实现灵活的页面结构和外观定制。
在模板继承中通常有两个主要角色
基础模板父模板 包含页面的整体结构、布局和通用元素。基础模板中定义了一些块block这些块表示可以被子模板覆盖或扩展的区域。子模板 继承基础模板并提供具体的内容。子模板通过定义和填充基础模板中的块来定制页面的特定部分。
案例
----------base.html----------
!DOCTYPE html
html langen
headmeta charsetUTF-8title{% block title %}Default Title{% endblock %}/title
/head
bodydiv idcontent{% block content %}{% endblock %}/div
/body
/html
-------------child.html-------------
{% extends base.html %}{% block title %}Child Title{% endblock %}{% block content %}h1Welcome to the child page/h1pThis is the content of the child page./p
{% endblock %}
子模板 child.html 使用了 {% extends base.html %} 来声明继承自 base.html然后通过 {% block title %}...{% endblock %} 和 {% block content %}...{% endblock %} 来定义或覆盖基础模板中的块。后端
package mainimport (html/templatenet/httpgithub.com/gin-gonic/gin
)var templates *template.Templatefunc init() {// 解析所有模板文件templates template.Must(template.ParseGlob(templates/*.html))
}func main() {// 创建Gin引擎router : gin.Default()// 子模板路由router.GET(/child, func(c *gin.Context) {// 渲染 child.htmlrenderTemplate(c, child.html, nil)})// 启动服务器router.Run(:8080)
}func renderTemplate(c *gin.Context, tmpl string, data interface{}) {// 执行模板渲染err : templates.ExecuteTemplate(c.Writer, tmpl, data)if err ! nil {// 处理错误c.AbortWithError(http.StatusInternalServerError, err)}
}
原文链自:https://lz.chatallshop.top/?p156