go-zero icon indicating copy to clipboard operation
go-zero copied to clipboard

When i DIY handle.tpl, it's not work well.

Open CockyCat opened this issue 1 year ago • 5 comments

I have according this document: https://go-zero.dev/cn/docs/advance/template to DIY my api output,but it's fully copy handle.tpl to internal/handler/xxxhandler.go

I think document maybe have some mistakes. when i adjust some code it work well:

package {{.PkgName}}                                                                                                                   

import (
        "net/http"
        "github.com/zeromicro/go-zero/rest/httpx"
        "xxxxx/common/response"
        {{.ImportPackages}}
)

func {{.HandlerName}}(ctx *svc.ServiceContext) http.HandlerFunc {
        return func(w http.ResponseWriter, r *http.Request) {
                {{if .HasRequest}}var req types.{{.RequestType}}
                if err := httpx.Parse(r, &req); err != nil {
                        httpx.Error(w, err)
                                return
                }{{end}}

                l := logic.New{{.LogicType}}(r.Context(), ctx)
                {{if .HasResp}}resp, {{end}}err := l.{{.Call}}({{if .HasRequest}}&req{{end}})
                {{if .HasResp}}response.Response(w, resp, err){{else}}response.Response(w, nil, err){{end}}

        }   
}

CockyCat avatar Jul 08 '22 02:07 CockyCat

What's the error? and what did you adjust?

kevwan avatar Jul 08 '22 14:07 kevwan

As my description that fully copy handle.tpl to internal/handler/xxxhandler.go, I mean as {% raw %}, {% endraw %} in xxxhandler.go , i think these should't appear in xxxhandle.go

CockyCat avatar Jul 09 '22 02:07 CockyCat

I have a commit to adjust that:

https://github.com/CockyCat/portal/commit/29e4c147ba688165ccb2e1d4da1c4fcb29ba4173

CockyCat avatar Jul 10 '22 03:07 CockyCat

Try and see https://pkg.go.dev/text/template

The input text for a template is UTF-8-encoded text in any format. "Actions"--data evaluations or control structures--are delimited by "{{" and "}}"

It's clear that delimited by "{%" and "%}" was not supported because we haven't custom delimiter in goctl.

kesonan avatar Jul 11 '22 15:07 kesonan

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Aug 11 '22 02:08 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar Nov 09 '22 02:11 github-actions[bot]