V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
Bluek404
V2EX  ›  分享创造

go2html5 —— 用 go 写 html

  •  1
     
  •   Bluek404 · 2014-12-29 21:52:53 +08:00 · 4467 次点击
    这是一个创建于 3411 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Github: https://github.com/Bluek404/go2html5

    Go Walker GoDoc

    脑洞产物

    用go写html

    除了普通的标签外

    还可以直接写控件

    所有函数均返回转换为HTML后的string

    HTML5的所有标签正在逐步添加中

    如果想实现自定义标签

    可以自己写函数

    例如:

    func CustomTag(attributes Attr, html []string) string {
        var tag = "custom-tag"
        var s bytes.Buffer
        s.WriteString("<" + tag)
        for k, v := range attributes {
            s.WriteString(" " + k + "=\"" + v + "\"")
        }
        s.WriteString(">")
        for _, v := range html {
            s.WriteString(v)
        }
        s.WriteString("</" + tag + ">")
        return s.String()
    }
    

    实现自定义控件:

    // 自定义按钮
    func CustomButton(href, name string, color int) string {
        var c string
        switch color {
        case 0:
            c = "btn-red"
        case 1:
            c = "btn-yellow"
        case 2:
            c = "btn-green"
        case 3:
            c = "btn-blue"
        default:
            c = "btn-red"
        }
    
        return "<a href=\"" + href + "\" class=\"btn" + c + "\">" + name +"</a>"
    }
    

    Example

    Html(Attr{"lang": "cn"},
        Head(nil,
            Title(nil, "Go2HTML5 Example"),
        ),
        Body(nil,
            P(nil,
                "现在是:",
                func() string {
                    var s bytes.Buffer
                    t := time.Now().Hour()
                    switch {
                    case t >= 0 && t <= 4:
                        s.WriteString("凌晨")
                    case t >= 5 && t <= 7:
                        s.WriteString("早上")
                    case t >= 8 && t < 10:
                        s.WriteString("上午")
                    case t >= 11 && t <= 13:
                        s.WriteString("中午")
                    case t >= 14 && t <= 19:
                        s.WriteString("下午")
                    case t >= 20 && t <= 22:
                        s.WriteString("晚上")
                    case t >= 23 && t <= 24:
                        s.WriteString("深夜")
                    }
                    s.WriteString(time.Now().Format("3点4分5秒"))
                    return s.String()
                }(),
            ),
            Hr(nil),
            P(nil,
                "Written in ", A(Attr{"href": "http://golang.org"}, "Go!"),
            ),
        ),
    )
    
    16 条回复    2015-01-01 01:06:32 +08:00
    kzzhr
        1
    kzzhr  
       2014-12-29 22:04:37 +08:00
    之前也有个类似的想法 。用JSON写html。。
    lidashuang
        2
    lidashuang  
       2014-12-29 22:46:53 +08:00
    那你得写多少函数啊。。。
    ETiV
        3
    ETiV  
       2014-12-29 22:49:05 +08:00 via iPhone
    为LZ的创造力鼓掌
    14
        4
    14  
       2014-12-29 22:51:08 +08:00 via Android
    go2sleep
    immjun
        5
    immjun  
       2014-12-29 22:52:29 +08:00
    期待更多分享~
    scarlex
        6
    scarlex  
       2014-12-29 22:54:47 +08:00
    何苦为难自己呢
    bcxx
        7
    bcxx  
       2014-12-29 22:59:05 +08:00
    哈哈哈哈好累啊…… go 这表达能力不适合做 DSL 啊
    Bluek404
        8
    Bluek404  
    OP
       2014-12-29 23:16:00 +08:00
    @lidashuang
    现在写了40个,还有70个……
    不过也就是个别名而已,所以不算麻烦,麻烦的是我把所有标签的使用方法和应用场景一起写上了。。坑不小心挖大了
    @bcxx
    还算能用吧
    lidashuang
        9
    lidashuang  
       2014-12-29 23:17:47 +08:00
    @Bluek404 写这种东西还是clojure/lisp牛逼
    kookxiang
        10
    kookxiang  
       2014-12-29 23:20:57 +08:00
    @Bluek404 估计你看了polymer会哭的
    Bluek404
        11
    Bluek404  
    OP
       2014-12-29 23:23:39 +08:00
    @kookxiang polymer我写过(dart的),编译出来太大
    而且不是很喜欢那个编码风格
    yegle
        12
    yegle  
       2014-12-30 02:15:57 +08:00   ❤️ 1
    要么 import . "github.com/Bluek404/go2html5" 把所有函数都引入然后能实现你给的例子那样的代码(但是目测有Address之类很通用的函数名),要么是引入一个别名然后所有函数调用都加上XX.Html这样的前缀。

    另外建议看看是不是能用go generate自动生成一些函数吧
    Bluek404
        13
    Bluek404  
    OP
       2014-12-30 02:32:14 +08:00 via Android
    @yegle 冲突这个的确没办法,其实本来想把tag属性也写一遍的,但是直接关键字冲突了……

    go generate的话我研究研究,还没用过
    Lucups
        14
    Lucups  
       2014-12-30 03:04:57 +08:00
    已 star,Good Job!
    horsley
        15
    horsley  
       2015-01-01 00:28:43 +08:00
    创意不错实现略搓,支持一下~
    Bluek404
        16
    Bluek404  
    OP
       2015-01-01 01:06:32 +08:00
    @horsley 其实本来想用结构体实现……但是还要再多写个数组……
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   787 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 20:38 · PVG 04:38 · LAX 13:38 · JFK 16:38
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.