GoのWebフレームワークEchoにテンプレートエンジンAceを導入する方法です。
Ace Proxyを使うことで、Load時のオプションをキャッシュしています。
server.go
package main
import (
"net/http"
"github.com/labstack/echo"
)
var e = echo.New()
func main() {
e.GET("/", func(c echo.Context) error {
return c.Render(http.StatusOK, "hello", "World")
})
e.Start(":8080")
}
template.go
package main
import (
"io"
"github.com/yosssi/ace"
"github.com/yosssi/ace-proxy"
"github.com/labstack/echo"
)
type AceTemplate struct {
AceProxy *proxy.Proxy
}
func (at *AceTemplate) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
tpl, err := at.AceProxy.Load(name, "", nil)
if err != nil {
return err
}
return tpl.Execute(w, data)
}
func init() {
at := &AceTemplate{
AceProxy: proxy.New(&ace.Options{
BaseDir: "template",
DynamicReload: true,
}),
}
e.Renderer = at
}
template/hello.ace
= doctype html
html lang=en
head
meta charset=utf-8
title Ace-Test
body
h1 Ace-Test
p
| Hello, {{.}}!


