getting started with templates in gin framework

  • Using the gin framework,return string types,JSON types,XML types ,struct types,JSONP type data.
  • the use of template and template syntax
  • Access mysql using the ORM package

 the use of template and template syntax

#The first template exercise

#1.Create main.go

package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	r := gin.Default()
	//Configure template engine to load everything in the template
	//r.loadHTMLFiles("templates/template1.html","templates/template2.html")
	//r.LoadHTMLGlob("templates/**/*")
	r.LoadHTMLGlob("template/*")
	//Render templates using the gin framework
	//Note: r.LoadHTMLGlob ("templates/*") needs to be configured
	//Use custom templates in gin and render background data to templates
	r.GET("/template", func(c *gin.Context) {
		c.HTML(http.StatusOK, "index.html", gin.H{
			"title": "Bind the background data to the front-end template, and then render it",
		})
	})
	r.Run(":8088")
}

#Create template/index.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<!--Bind the back-end title data return value to the front end-->
  {{.title}}
</body>
</html>

#Execute:

go run main.go

//Execute result:
//Listening and serving HTTP on :8088
//[GIN] 2022/11/03 - 11:29:51 | 200 |    1.019759ms |       127.0.0.1 | GET      //"/template"

 curl http://localhost:8088/template

// Bind the background data to the front-end template, and then render it 

#2.Template group exercise

Template rendering and template Syntax of gin Framework

#How to render multiple groups of templates to the front end?

#Modify the parameters introduced into the template engine

r.LoadHTMLGlob("templates/**/*")

#Declare in the template file that the template group to which the template belongs
default/index.html

{{define "templates/news/index.html"}}
    Template content
{{end}}

create templates/news/index.html

{{define "news/index.html"}}

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<ul>
    {{range $key,$value:=.newsList}}
    <li>{{$key}}:{{$value.Title}}</li>
    <li>{{$key}}:{{$value.Content}}</li>
    {{end}}
</ul>
</ul>
</body>
</html>
{{end}}

#Create main.go

package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

type Article struct {
	Title   string `json:"title"`
	Content string `json:"content"`
}

func main() {
	r := gin.Default()
	//Configure template engine to load everything in the template
	//r.loadHTMLFiles("templates/template1.html","templates/template2.html")
	//r.LoadHTMLGlob("templates/**/*")
	r.LoadHTMLGlob("templates/**/*")
	//Render templates using the gin framework
	//Note: r.LoadHTMLGlob ("templates/*") needs to be configured
	//Use custom templates in gin and render background data to templates
	r.GET("/news", func(c *gin.Context) {
		c.HTML(http.StatusOK, "news/index.html", gin.H{
			"newsList": []interface{}{
				&Article{
					Title:   "news title1",
					Content: "news content1",
				},
				&Article{
					Title:   "news title2",
					Content: "news content2",
				},
			},
			"hobby": []string{"eatting", "sleeping", "write code", "meditate"},
		})
	})
	r.Run(":8088")
}

#Execute:

go run main.go

//Execute result:
//Listening and serving HTTP on :8088
//[GIN] 2022/11/03 - 13:02:17 | 200 |     727.271µs |       127.0.0.1 | GET      //"/news"
//[GIN] 2022/11/03 - 13:03:01 | 200 |     739.325µs |       127.0.0.1 | GET      //"/news"

curl http://localhost:8088/news

//Execute result:
//0:{news title1 news content1} 1:{news title2 news content2}
//0:news title1
//0:news content1
//1:news title2
//1:news content2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 在引用\[1\]和引用\[2\]中,提到了使用templates来避免代码重复和节省时间的方法。在编写template时,可以将共同的部分提取出来,以避免重复的代码。例如,在引用\[1\]中,通过编写一个template来处理固定尺寸的正方矩阵,其中包括支持逆矩阵运算的功能。而在引用\[2\]中,提到了编写一个函数模板,接受一个迭代器,并对迭代器指向的对象进行操作。这些都是使用templates来实现代码重用的例子。 在引用\[3\]中,提到了使用templates生成多个classes和多个函数的情况。为了避免代码膨胀,需要确保template代码与造成膨胀的template参数之间没有相互依赖关系。对于非类型模板参数造成的代码膨胀,可以通过使用函数参数或class成员变量来替换template参数来消除。而对于类型参数造成的代码膨胀,可以通过让具现化类型共享实现码来降低代码膨胀。 综上所述,templates是一种用于避免代码重复和节省时间的方法,可以在编写类和函数时使用。 #### 引用[.reference_title] - *1* *2* *3* [C++进阶_Effective_C++第三版(七) 模板与泛型编程 Templates and Generic Programming](https://blog.csdn.net/weixin_39407199/article/details/106283371)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值