From 87f639da586e5e3224da1d5f4520247af55914b1 Mon Sep 17 00:00:00 2001 From: xing Date: Sat, 6 May 2023 23:00:43 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/plugins/devexample/plugintt/a.gohtml | 9 +++ app/plugins/devexample/plugintt/main.go.dev | 82 ++++++++++++++++++--- app/plugins/devexample/plugintt/make.sh | 3 +- 3 files changed, 81 insertions(+), 13 deletions(-) create mode 100644 app/plugins/devexample/plugintt/a.gohtml diff --git a/app/plugins/devexample/plugintt/a.gohtml b/app/plugins/devexample/plugintt/a.gohtml new file mode 100644 index 0000000..dcde123 --- /dev/null +++ b/app/plugins/devexample/plugintt/a.gohtml @@ -0,0 +1,9 @@ +
+ {{.aa}} +
+ +
+ {{ range $k,$v := .posts}} +

{{$v.PostTitle}}

+ {{end}} +
\ No newline at end of file diff --git a/app/plugins/devexample/plugintt/main.go.dev b/app/plugins/devexample/plugintt/main.go.dev index 98070da..78a581b 100644 --- a/app/plugins/devexample/plugintt/main.go.dev +++ b/app/plugins/devexample/plugintt/main.go.dev @@ -1,24 +1,82 @@ package main import ( + "embed" + "github.com/fthvgb1/wp-go/app/cmd/route" "github.com/fthvgb1/wp-go/app/pkg/constraints" + "github.com/fthvgb1/wp-go/app/pkg/logs" + "github.com/fthvgb1/wp-go/app/plugins/wphandle" "github.com/fthvgb1/wp-go/app/theme" "github.com/fthvgb1/wp-go/app/theme/wp" + "github.com/fthvgb1/wp-go/app/theme/wp/components" + "github.com/fthvgb1/wp-go/app/theme/wp/components/widget" + route2 "github.com/fthvgb1/wp-go/app/theme/wp/route" + "github.com/gin-gonic/gin" + "html/template" + "net/http" "plugintt/xx" ) -func init() { - // here can register theme - theme.AddThemeHookFunc("xxx", Xo) -} +//go:embed a.gohtml +var em embed.FS +var tt *template.Template -func Xo(h *wp.Handle) { - // action or hook or config theme - h.PushHandler(constraints.PipeRender, constraints.Home, wp.HandleCall{ - Fn: func(handle *wp.Handle) { - xx.Xo() - }, - Order: 100, - Name: "xxxx", +func init() { + // register as theme + theme.AddThemeHookFunc("themename", hook) + + //use the local template + //note: must use embed.FS + t, err := template.ParseFS(em, "a.gohtml") + if err != nil { + logs.Error(err, "") + } + tt = t + + // register gin route. it will be effecting when server restart. + route.Hook(func(r *gin.Engine) { + r.GET("xx", func(c *gin.Context) { + c.String(http.StatusOK, "xxoo") + }) + }) +} + +func hook(h *wp.Handle) { + wp.Run(h, config) +} + +func config(h *wp.Handle) { + // same theme config + wphandle.UsePlugins(h) + wp.InitPipe(h) + h.PushHandler(constraints.PipeMiddleware, constraints.Home, + wp.NewHandleFn(widget.IsCategory, 100, "widget.IsCategory")) + components.WidgetArea(h) + h.PushHandler(constraints.PipeRender, constraints.Home, wp.NewHandleFn(func(h *wp.Handle) { + h.SetData("aa", "xyxxxx") + h.RenderHtml(tt, http.StatusOK, "a.gohtml") + h.Abort() + h.StopPipe() + }, 10, "renderHome")) + + // use simple reg route + route2.PushRoute(`(?P\w+)/(?P\w+)`, route2.Route{ + Path: `(?P\w+)/(?P\w+)`, + Scene: constraints.Home, + Method: []string{"GET"}, + Type: "reg", + ConfigHandle: wp.Index, + }) + //... +} + +// Xo to be a func when theme init +func Xo(h *wp.Handle) { + xx.Xo() + route2.Delete(`(?P\w+)/(?P\w+)`) + h.ReplaceHandle(constraints.PipeRender, "wp.RenderTemplate", func(h *wp.Handle) { + h.SetData("aa", "xyxxxx") + h.RenderHtml(tt, http.StatusOK, "a.gohtml") + h.StopPipe() }) } diff --git a/app/plugins/devexample/plugintt/make.sh b/app/plugins/devexample/plugintt/make.sh index bababac..27a751e 100644 --- a/app/plugins/devexample/plugintt/make.sh +++ b/app/plugins/devexample/plugintt/make.sh @@ -2,5 +2,6 @@ # copy plugintt to other dir and remove .dev suffix # note the go version and build tool flag must same to server build -# eg: -gcflags all="-N -l" may used in ide debug +# eg: -gcflags all="-N -l" --race may used in ide debug +go mod tidy go build -buildmode=plugin -o xx.so main.go \ No newline at end of file