golangWeb框架---github.com/gin-gonic/gin学习六(静态文件、模版、模版函数)

静态文件

package main

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

func main() {
	router := gin.Default()

	router.Static("/assets", "./assets")
	router.StaticFS("/more_static", http.Dir("assets"))
	router.StaticFile("/favicon.ico", "./assets/a.png")

	router.Run(":8080")
}

在我的项目下创建一个assets文件夹
在这里插入图片描述

直接上图就行:
1、访问more_static
在这里插入图片描述

2、访问assets下的某个文件
在这里插入图片描述

3、直接访问assets下的某个文件
在这里插入图片描述

下载服务端文件

我们就下载我们上例中的图片,路径为
http://127.0.0.1:8080/favicon.ico

package main

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

func main() {

	router := gin.Default()

	router.StaticFile("/favicon.ico", "./assets/a.png")

	router.GET("/someDataFromReader", func(c *gin.Context) {
		response, err := http.Get("http://127.0.0.1:8080/favicon.ico")
		if err != nil || response.StatusCode != http.StatusOK {
			c.Status(http.StatusServiceUnavailable)
			return
		}

		reader := response.Body
		contentLength := response.ContentLength
		contentType := response.Header.Get("Content-Type")

		extraHeaders := map[string]string{
			"Content-Disposition": `attachment; filename="gopher.png"`,
		}

		c.DataFromReader(http.StatusOK, contentLength, contentType, reader, extraHeaders)
	})

	router.Run(":8080")
}

浏览器输入http://127.0.0.1:8080/someDataFromReader 就实现了图片的下载

模版语言(1)

虽然现在业务开发,需要数据分离,但是web框架都有自己的模版语言,比如Django的Jinja2,Jinja2是基于python的模板引擎,功能比较类似于于PHP的smarty,J2ee的Freemarker和velocity。
今天就来了解下gin里面的是如何使用的,这些东西都很简单,直接上代码,看效果即可

func main() {

	router := gin.Default()

	router.LoadHTMLGlob("template/*")
	router.GET("/index", func(c *gin.Context) {
		c.HTML(http.StatusOK, "view.html", gin.H{
			"title": "Main website",
		})
	})

	router.Run(":8080")
}

看一下view.html代码

<html>
<h1>
{{ .title }}
</h1>
</html>

直接上效果图:
在这里插入图片描述

模版语言(2)

上一个例子的模版页面,是放到template的目录下,如果在template下有多级目录呢?比如下面这个截图
在这里插入图片描述

然后看后端代码如下:

func main() {

	router := gin.Default()

	router.LoadHTMLGlob("template/**/*")
	router.GET("/posts/index", func(c *gin.Context) {
		c.HTML(http.StatusOK, "index1.tmpl", gin.H{
			"title": "Posts",
		})
	})
	router.GET("/users/index", func(c *gin.Context) {
		c.HTML(http.StatusOK, "users/index2.tmpl", gin.H{
			"title": "Users",
		})
	})

	router.Run(":8080")
}

index1.tmpl

<html><h1>
	{{ .title }}
</h1>
<p>Using posts/index1.tmpl</p>
</html>

index2.tmpl

{{ define "users/index2.tmpl" }}
<html><h1>
	{{ .title }}
</h1>
<p>Using users/index2.tmpl</p>
</html>
{{ end }}

效果图如下:
在这里插入图片描述

在这里插入图片描述

自定义模版函数

我们还可以自己定制模版函数
You may use custom delims

r := gin.Default()
	r.Delims("{[{", "}]}")
	r.LoadHTMLGlob("/path/to/templates"))

设置router.Delims需要的格式,然后SetFuncMap设置需要的值函数,然后通过c.HTML渲染数据即可

代码比较简单,我都是直接上代码,看效果即可

package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"html/template"
	"net/http"
	"time"
)

func formatAsDate(t time.Time) string {
	year, month, day := t.Date()
	return fmt.Sprintf("%d/%02d/%02d", year, month, day)
}


func main() {

	router := gin.Default()

	router.Delims("{[{", "}]}")
	router.SetFuncMap(template.FuncMap{
		"formatAsDate": formatAsDate,
	})
	router.LoadHTMLFiles("template/a.html")

	router.GET("/raw", func(c *gin.Context) {
		c.HTML(http.StatusOK, "a.html", map[string]interface{}{
			"now": time.Date(2017, 07, 01, 0, 0, 0, 0, time.UTC),
		})
	})

	router.Run(":8080")
}

模版代码a.html

<body>

Date: {[{.now | formatAsDate}]}

</body>

最后看下效果图:
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值