我做了一个开源项目

最近研究了Golang Template 模板引擎,发现它自身提供的能力比较弱,很多常用的功能都没有,比如字符串替换、判断,base64编码解码等等。但是Golang Template给我们预留了自定义函数,也就是template.FuncMap,可以满足我们功能的扩展。

func main() {
    // First we create a FuncMap with which to register the function.
    funcMap := template.FuncMap{
        // The name "title" is what the function will be called in the template text.
        "title": strings.Title,
    }

    // A simple template definition to test our function.
    // We print the input text several ways:
    // - the original
    // - title-cased
    // - title-cased and then printed with %q
    // - printed with %q and then title-cased.
    const templateText = `
Input: {{printf "%q" .}}
Output 0: {{title .}}
Output 1: {{title . | printf "%q"}}
Output 2: {{printf "%q" . | title}}
`

    // Create a template, add the function map, and parse the text.
    tmpl, err := template.New("titleTest").Funcs(funcMap).Parse(templateText)
    if err != nil {
        log.Fatalf("parsing: %s", err)
    }

    // Run the template to verify the output.
    err = tmpl.Execute(os.Stdout, "the go programming language")
    if err != nil {
        log.Fatalf("execution: %s", err)
    }

}

以上是一个Go语言 Template中的例子,自定义扩展了一个title函数,它的能力通过strings.Title实现。然后我们通过如下代码生成一个template.FuncMap:

    funcMap := template.FuncMap{
        // The name "title" is what the function will be called in the template text.
        "title": strings.Title,
    }

有了funcMap就可以通过template的Funcs方法注册这个扩展的函数了,这样我们就可以在模板里使用它。

const templateText = `
Input: {{printf "%q" .}}
Output 0: {{title .}}
Output 1: {{title . | printf "%q"}}
Output 2: {{printf "%q" . | title}}
`

以上就是一个通过自定义函数增强Golang Template的例子,这样就可以扩展Golang Template的能力。

最开始的想法是通过Golang Template做一个开源项目,把常用的模板函数能力都实现了,但是又怕重复造轮子,所以就去Github上搜索有没有已经实现好的库,搜索了一圈没有找到(大家找到了记得告我我)。

没有找到就只能自己做了,在开始写之前,又想起来HUGO这个开源项目,我自己的博客就是用这个构建的。HUGO为了支持博客的自定义,所以内置了很多强大的模板函数,既然这样,可以把HUGO的这些模板函数抽取出来,进行修改和增强,这样就可以形成了一个日常使用的工具库了。

然后周末晚上,夜深人静的时候,着手准备,最终花了近4个小时的时间,抽取了一个beta版本。隔了几天,又在一个夜深人静的黑夜,完善了相关代码和文档,最终开源到了Github上。

开源库地址:https://github.com/flysnow-org/soha

开源库地址:https://github.com/flysnow-org/soha

这个库的使用非常简单,首先你需要导入这个库 import "github.com/flysnow-org/soha"
然后通如下函数获取一个FuncMap

sohaFuncMap := soha.CreateFuncMap()

有了FuncMap就非常容易了,如果你使用内置的template,那么只需要调用Funcs方法即可。

tmpl, err := template.New("titleTest").Funcs(sohaFuncMap).Parse(templateText)

如果你使用的是gin这类Golang Web框架,那么只需要使用如下代码注册即可:

    router := gin.Default()

    sohaFuncMap := soha.CreateFuncMap()
    router.SetFuncMap(sohaFuncMap)

这里有一个Hello World,大家可以参考。

package main

import (
    "github.com/flysnow-org/soha"
    "html/template"
    "log"
    "os"
)
func main() {

    sohaFuncMap := soha.CreateFuncMap()
    const templateText = `{{md5 .}}`

    tmpl, err := template.New("titleTest").Funcs(sohaFuncMap).Parse(templateText)
    if err != nil {
        log.Fatalf("parsing: %s", err)
    }

    err = tmpl.Execute(os.Stdout, 123456)
    if err != nil {
        log.Fatalf("execution: %s", err)
    }

}

SOHA 为Golang Template增强了五六十个函数,涉及到字符串、URL、转换、hash、base64、math、集合等方方面面,让你的Golang Web开发事半功倍。每个函数的说明和示例等请参考github repo库,都有详细的说明,中英文文档都有。

SOHA 因为从HUGO里抽取了代码,所以继续保留了HUGO的 Apache Licence。欢迎大家star、issue、pr等贡献,多多使用。

一切都是为了,不再996。

往期精彩回顾

不到50行代码实现公众号二维码动画

Go语言生成二维码是如此简单


Go语言(Golang)环境搭建详解

扫码关注

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值