Go 每日一庫之 quicktemplate

簡介

最近在整理咱們項目代碼的時候,發現有不少活動的代碼在結構和提供的功能上都很是類似。爲了方便從此的開發,我花了一點時間編寫了一個生成代碼框架的工具,最大程度地下降重複勞動。代碼自己並不複雜,且與項目代碼關聯性較大,這裏就不展開介紹了。在這個過程當中,我發現 Go 標準的模板庫text/templatehtml/template使用起來比較束手束腳,很不方便。我從 GitHub 瞭解到quicktemplate這個第三方模板庫,功能強大,語法簡單,使用方便。今天咱們就來介紹一下quicktemplatehtml

快速使用

本文代碼使用 Go Modules。git

先建立代碼目錄並初始化:github

$ mkdir quicktemplate && cd quicktemplate
$ go mod init github.com/darjun/go-daily-lib/quicktemplate

quicktemplate會將咱們編寫的模板代碼轉換爲 Go 語言代碼。所以咱們須要安裝quicktemplate包和一個名爲qtc的編譯器:golang

$ go get -u github.com/valyala/quicktemplate
$ go get -u github.com/valyala/quicktemplate/qtc

首先,咱們須要編寫quicktemplate格式的模板文件,模板文件默認以.qtpl做爲擴展名。下面我編寫了一個簡單的模板文件greeting.qtplweb

All text outside function is treated as comments.

{% func Greeting(name string, count int) %}
  {% for i := 0; i < count; i++ %}
    Hello, {%s name %}
  {% endfor %}
{% endfunc %}

模板語法很是簡單,咱們只須要簡單瞭解如下 2 點:瀏覽器

  • 模板以函數爲單位,函數能夠接受任意類型和數量的參數,這些參數能夠在函數中使用。全部函數外的文本都是註釋,qtc編譯時會忽視註釋;
  • 函數內的內容,除了語法結構,其餘都會原樣輸出到渲染後的文本中,包括空格和換行

greeting.qtpl保存到templates目錄,而後執行qtc命令。該命令會生成對應的 Go 文件greeting.qtpl.go,包名爲templates。如今,咱們就可使用這個模板了:安全

package main

import (
  "fmt"

  "github.com/darjun/go-daily-lib/quicktemplate/get-started/templates"
)

func main() {
  fmt.Println(templates.Greeting("dj", 5))
}

調用模板函數,傳入參數,返回渲染後的文本:服務器

$ go run .


    Hello, dj

    Hello, dj

    Hello, dj

    Hello, dj

    Hello, dj

{%s name %}執行文本替換,{% for %}循環生成重複文本。輸出中出現多個空格和換行,這是由於函數內除了語法結構,其餘內容都會原樣保留,包括空格和換行微信

須要注意的是,因爲quicktemplate是將模板轉換爲 Go 代碼使用的,因此若是模板有修改,必須先執行qtc命令從新生成 Go 代碼,不然修改不生效框架

語法結構

quicktemplate支持 Go 常見的語法結構,if/for/func/import/return。並且寫法與直接寫 Go 代碼沒太大的區別,幾乎沒有學習成本。只是在模板中使用這些語法時,須要使用{%%}包裹起來,並且iffor等須要添加endif/endfor明確表示結束。

變量

上面咱們已經看到如何渲染傳入的參數name,使用{%s name %}。因爲name是 string 類型,因此在{%後使用s指定類型。quicktemplate還支持其餘類型的值:

  • 整型:{%d int %}{%dl int64 %}{%dul uint64 %}
  • 浮點數:{%f float %}。還能夠設置輸出的精度,使用{%f.precision float %}。例如{%f.2 1.2345 %}輸出1.23
  • 字節切片([]byte):{%z bytes %}
  • 字符串:{%q str %}或字節切片:{%qz bytes %},引號轉義爲&quot;
  • 字符串:{%j str %}或字節切片:{%jz bytes %},沒有引號;
  • URL 編碼:{%u str %}{%uz bytes %}
  • {%v anything %}:輸出等同於fmt.Sprintf("%v", anything)

先編寫模板:

{% func Types(a int, b float64, c []byte, d string) %}
  int: {%d a %}, float64: {%f.2 b %}, bytes: {%z c %}, string with quotes: {%q d %}, string without quotes: {%j d %}.
{% endfunc %}

而後使用:

func main() {
  fmt.Println(templates.Types(1, 5.75, []byte{'a', 'b', 'c'}, "hello"))
}

運行:

$ go run .

  int: 1, float64: 5.75, bytes: abc, string with quotes: &quot;hello&quot;, string without quotes: hello.

調用函數

quicktemplate支持在模板中調用模板函數、標準庫的函數。因爲qtc會直接生成 Go 代碼,咱們甚至還能夠在同目錄下編寫本身的函數給模板調用,模板 A 中也能夠調用模板 B 中定義的函數。

咱們先在templates目錄下編寫一個文件rank.go,定義一個Rank函數,傳入分數,返回評級:

package templates

func Rank(score int) string {
  if score >= 90 {
    return "A"
  } else if score >= 80 {
    return "B"
  } else if score >= 70 {
    return "C"
  } else if score >= 60 {
    return "D"
  } else {
    return "E"
  }
}

而後咱們能夠在模板中調用這個函數:

{% import "fmt" %}
{% func ScoreList(name2score map[string]int) %}
  {% for name, score := range name2score %}
    {%s fmt.Sprintf("%s: score-%d rank-%s", name, score, Rank(score)) %}
  {% endfor %}
{% endfunc %}

編譯模板:

$ qtc

編寫程序:

func main() {
  name2score := make(map[string]int)
  name2score["dj"] = 85
  name2score["lizi"] = 96
  name2score["hjw"] = 52

  fmt.Println(templates.ScoreList(name2score))
}

運行程序輸出:

$ go run .


    dj: score-85 rank-B

    lizi: score-96 rank-A

    hjw: score-52 rank-E

因爲咱們在模板中用到fmt包,須要先使用{% import %}將該包導入。

在模板中調用另外一個模板的函數也是相似的,由於模板最終都會轉爲 Go 代碼。Go 代碼中有一樣簽名的函數。

Web

quicktemplate經常使用來編寫 HTML 頁面的模板:

{% func Index(name string) %}
<html>
  <head>
    <title>Awesome Web</title>
  </head>
  <body>
    <h1>Hi, {%s name %}
    <p>Welcome to the awesome web!!!</p>
  </body>
</html>
{% endfunc %}

下面編寫一個簡單的 Web 服務器:

func index(w http.ResponseWriter, r *http.Request) {
  templates.WriteIndex(w, r.FormValue("name"))
}

func main() {
  mux := http.NewServeMux()
  mux.HandleFunc("/", index)

  server := &http.Server{
    Handler: mux,
    Addr:    ":8080",
  }

  log.Fatal(server.ListenAndServe())
}

qtc會生成一個Write*的方法,它接受一個io.Writer的參數。將模板渲染的結果寫入這個io.Writer中,咱們能夠直接將http.ResponseWriter做爲參數傳入,很是便捷。

運行:

$ qtc
$ go run .

瀏覽器輸入localhost:8080?name=dj查看結果。

總結

quicktemplate至少有下面 3 個優點:

  • 語法與 Go 語言很是相似,幾乎沒有學習成本;
  • 會先轉換爲 Go,渲染速度很是快,比標準庫html/template快 20 倍以上;
  • 爲了安全考慮,會執行一些編碼,避免受到攻擊。

從我我的的實際使用狀況來看,確實很方便,很實用。感興趣的還能夠去看看qtc生成的 Go 代碼。

你們若是發現好玩、好用的 Go 語言庫,歡迎到 Go 每日一庫 GitHub 上提交 issue😄

參考

  1. quicktemplate GitHub:https://github.com/valyala/quicktemplate
  2. Go 每日一庫 GitHub:https://github.com/darjun/go-daily-lib

個人博客:https://darjun.github.io

歡迎關注個人微信公衆號【GoUpUp】,共同窗習,一塊兒進步~

相關文章
相關標籤/搜索