go - 通过堆栈管理和提供 html/模板数据绑定(bind)

标签 go

有没有人在使用 Go 的 html/template 时实现了管理 View 绑定(bind)的解决方案?具体来说,我希望找到能让我做类似事情的东西:

  • 在安装过程中设置全局 Site.Title
  • 提供请求范围的变量,如 CurrentURL
  • 然后,在 Render 步骤中,只需提供特定于 http.Handler 的变量,然后将其组合并提供给模板。

现有应用程序的示例如下所示(我使用 unrolled/render 进行布局继承,但这是可替换的):

package main

import (
    "log"
    "net"
    "net/http"
    "os"
    "strings"

    "github.com/go-chi/chi"
    "github.com/go-chi/chi/middleware"
    "github.com/unrolled/render"
)

type HelloBinding struct {
    Name string
}

func helloHandler(render *render.Render) http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
        _ = render.HTML(w, http.StatusOK, "hello", &HelloBinding{
            Name: "World!",
        })
    }
}

func main() {
    port, ok := os.LookupEnv("PORT")
    if !ok {
        port = "8080"
    }

    render := render.New(render.Options{
        Directory:     "templates",
        Layout:        "layout",
        Extensions:    []string{".html"},
        IsDevelopment: true,
    })

    r := chi.NewMux()

    r.Use(middleware.Logger)
    r.Use(middleware.Recoverer)

    r.Get("/", helloHandler(render))

    httpServer := &http.Server{
        Addr:    net.JoinHostPort("", port),
        Handler: r,
    }

    log.Printf("http server listening at %s\n", httpServer.Addr)
    if err := httpServer.ListenAndServe(); err != nil {
        log.Panic(err)
    }
}

<html>
<head>
    <title></title>
</head>
<body>

    {{ yield }}

</body>
</html>

共享 View

Hello, {{ .Name }}

在理想的解决方案中,这样的事情是可能的:

Warning: Pseudo code

package main

import (
    "log"
    "net"
    "net/http"
    "os"
    "strings"

    "github.com/go-chi/chi"
    "github.com/go-chi/chi/middleware"
    "github.com/unrolled/render"
)

type GlobalBinding struct {
    Title string
}

type RequestBinding struct {
    CurrentURL string
}

type HelloBinding struct {
    Name string
}

func helloHandler(render *render.Render) http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
        _ = render.HTML(w, http.StatusOK, "hello", &HelloBinding{
            Name: "World!",
        })
    }
}

func main() {
    port, ok := os.LookupEnv("PORT")
    if !ok {
        port = "8080"
    }

    render := render.New(render.Options{
        Directory:     "templates",
        Layout:        "layout",
        Extensions:    []string{".html"},
        IsDevelopment: true,
    })

    // Binds data to be used
    render.Bind(GlobalBindings{
        Title: "My Site",
    })

    r := chi.NewMux()

    r.Use(middleware.Logger)
    r.Use(middleware.Recoverer)

    // Binds data for the request context only
    r.Use(func(next http.Handler) http.Handler {
        return func(w http.ResponseWriter, r *http.Request) {
            render.BindContext(r.Context, RequestBinding{
                CurrentURL: r.URL.String(),
            })
            next(w, r)
        }
    })

    r.Get("/", helloHandler(render))

    httpServer := &http.Server{
        Addr:    net.JoinHostPort("", port),
        Handler: r,
    }

    log.Printf("http server listening at %s\n", httpServer.Addr)
    if err := httpServer.ListenAndServe(); err != nil {
        log.Panic(err)
    }
}

允许我将布局更改为:

<html>
<head>
    <title>{{ .Global.Title }}</title>
</head>
<body>

    {{ .CurrentURL }}

    {{ yield }}

</body>
</html>

并且各个处理程序在不考虑太多的情况下合并和绑定(bind)。


希望你们有一些解决办法!我为此苦苦挣扎了一段时间。

最佳答案

不在 html/template 但考虑使用 quicktemplates

https://github.com/valyala/quicktemplate

它是围绕常规代码设计的,因此您的渲染器只是可以采用任意参数(和接口(interface))的函数。您可以导入包并调用常规函数并访问全局变量。

与内置的模板引擎相比,它使用起来更舒适,而且您还可以进行静态类型检查。唯一的缺点是您需要在编辑后重建/重新启动以反射(reflect)更改。

关于go - 通过堆栈管理和提供 html/模板数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54134591/

相关文章:

http.ListenAndServe 仅适用于本地主机?

go - Kubernetes 去客户端 : list events

go - 如何在另一个 map 界面中添加一个 map 界面?

linux - TERM=dumb 终端必须具备的功能

go - 我如何等待对多个其他 Goroutines 的单个 Goroutine 响应?

go - 在 Go 中旋转数组

pointers - Go Programming - 使用指针绕过访问权限

Go routine 不接收通过 channel 发送的所有数据——玩具示例程序

c - 以字节为单位在 golang 中打包结构以与 C 应用程序通信

bash - 如何启动并运行 sh 格式化程序?