golang pongo2 function 的问题

2021-06-20 15:27:05 +08:00
 chenqh

html


                <div class="layui-form-item">
                    {{form_label("登陆绑定 IP" ,label_cls)}}
                    <div class="layui-input-inline" data-name="{{optionSessionBindIp.Name}}" data-value="optionSessionBindIp.Value">
                        {{switch_option_render(optionSessionBindIp, fn_format("/a/admin/setting/update?name=%s",option.Name) )}}

                    </div>
                </div><!-- 登录绑定 IP -->

fn_format 的代码


func GinContextExtend(c *gin.Context, dataList ...pongo2.Context) pongo2.Context {
	// 可以把获取当前用户放在这里
	d := pongo2.Context{
		"fn_format": fmt.Sprintf,
	}
	user, err := service.GinGetUser(c)
	if err == nil {
		d["user"] = user
		menuList, err := ReadMenuList(user.Role)
		if err != nil {
			panic(err)
		}
		menuListByte, err := json.Marshal(menuList)
		if err != nil {
			panic(err)
		}
		d["menuList"] = string(menuListByte)
	}

	var newList = []pongo2.Context{
		d,
	}
	newList = append(newList, dataList...)

	return ContextExtend(newList...)
}

现在运行报错


2021/06/20 15:18:05 [Recovery] 2021/06/20 - 15:18:05 panic recovered:
[Error (where: execution) in /home/vagrant/gcode/gadmin/templates/admin/setting.html | Line 26 Col 27 near 'switch_option_render'] [Error (where: execution) in /home/vagrant/gcode/gadmin/templates/admin/setting.html | Line 26 Col 69 near 'fn_format'] Calling a function using an invalid parameter
/home/vagrant/go/pkg/mod/github.com/gin-gonic/gin@v1.7.2/context.go:914 (0x9f703b)
        (*Context).Render: panic(err)
/home/vagrant/go/pkg/mod/github.com/gin-gonic/gin@v1.7.2/context.go:923 (0x9f70f8)
        (*Context).HTML: c.Render(code, instance)
/home/vagrant/gcode/gadmin/router/page_router/page_admin_setting.go:45 (0xb0c3d3)
        PageAdminSetting.func1: c.HTML(200, "admin/setting.html", render.GinContextExtend(c, pongo2.Context{
/home/vagrant/gcode/gadmin/router/page_router/page_admin_setting.go:54 (0xb0bcae)
        PageAdminSetting: }()
/home/vagrant/go/pkg/mod/github.com/gin-gonic/gin@v1.7.2/context.go:165 (0xada3e6)
        (*Context).Next: c.handlers[c.index](c)
/home/vagrant/gcode/gadmin/pkg/middleware/middleware.go:73 (0xada3c5)
        LoginRequired.func1.1: c.Next()
/home/vagrant/gcode/gadmin/pkg/middleware/middleware.go:77 (0xadaa89)
        LoginRequired.func1: }()
/home/vagrant/go/pkg/mod/github.com/gin-gonic/gin@v1.7.2/context.go:165 (0xa06f73)
        (*Context).Next: c.handlers[c.index](c)
/home/vagrant/go/pkg/mod/github.com/gin-gonic/gin@v1.7.2/logger.go:241 (0xa06f32)
        LoggerWithConfig.func1: c.Next()
/home/vagrant/go/pkg/mod/github.com/gin-gonic/gin@v1.7.2/context.go:165 (0xa07e99)
        (*Context).Next: c.handlers[c.index](c)
/home/vagrant/go/pkg/mod/github.com/gin-gonic/gin@v1.7.2/recovery.go:99 (0xa07e80)
        CustomRecoveryWithWriter.func1: c.Next()
/home/vagrant/go/pkg/mod/github.com/gin-gonic/gin@v1.7.2/context.go:165 (0xa06f73)
        (*Context).Next: c.handlers[c.index](c)
/home/vagrant/go/pkg/mod/github.com/gin-gonic/gin@v1.7.2/logger.go:241 (0xa06f32)
        LoggerWithConfig.func1: c.Next()
/home/vagrant/go/pkg/mod/github.com/gin-gonic/gin@v1.7.2/context.go:165 (0x9fd429)
        (*Context).Next: c.handlers[c.index](c)
/home/vagrant/go/pkg/mod/github.com/gin-gonic/gin@v1.7.2/gin.go:489 (0x9fd40f)
        (*Engine).handleHTTPRequest: c.Next()
/home/vagrant/go/pkg/mod/github.com/gin-gonic/gin@v1.7.2/gin.go:445 (0x9fcefb)
        (*Engine).ServeHTTP: engine.handleHTTPRequest(c)
/usr/local/go/src/net/http/server.go:2887 (0x7ac6c2)
        serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
/usr/local/go/src/net/http/server.go:1952 (0x7a7aec)
        (*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
/usr/local/go/src/runtime/asm_amd64.s:1371 (0x46e800)
        goexit: BYTE    $0x90   // NOP

该怎么弄呀, 在 macro 里面不能调用 function?

py 转来 golang 好不习惯呀

1136 次点击
所在节点    Go 编程语言
2 条回复
chenqh
2021-06-20 15:39:06 +08:00
我自己的问题, 我知道怎么解决了
x940727
2021-06-21 19:22:04 +08:00
go 的代码这样看就好丑啊……

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/784614

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX