跳过正文
  1. 文章/
  2. GoLang/
  3. GoWeb/
  4. Gin/

6、模板引擎

·160 字·1 分钟· loading · loading · ·
GoLang GoWeb Gin
GradyYoung
作者
GradyYoung
Gin - 点击查看当前系列文章
§ 6、模板引擎 「 当前文章 」

gin支持加载HTML模板, 然后根据模板参数进行配置并返回相应的数据,本质上就是字符串替换

基本使用
#

package main

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

func main() {
    router := gin.Default()
    // 加载所有模板文件
    router.LoadHTMLGlob("template/*")
    router.GET("/index", func(context *gin.Context) {
        // 渲染模板文件,第二个参数为模板在上面template文件夹中的路径,第三个参数为模板参数
        context.HTML(http.StatusOK, "index.html", "hello")
    })
    router.Run(":8080")
}

静态文件
#

例如jpg、js这些静态文件,可以指定路径前缀和路径

router.Static("static","./static")
Gin - 点击查看当前系列文章
§ 6、模板引擎 「 当前文章 」