From 8fa1cfe077967d2e15b8f6efd6e1e6e5c50c9167 Mon Sep 17 00:00:00 2001 From: shaoying Date: Thu, 31 May 2018 09:56:37 +0800 Subject: [PATCH] mac add funcmap --- src/contexts/web/web.go | 76 +++++++++++++++++++++++++++++++--- usr/template/common/base.html | 7 +++- usr/template/common/login.html | 2 +- 3 files changed, 77 insertions(+), 8 deletions(-) diff --git a/src/contexts/web/web.go b/src/contexts/web/web.go index 4b503b88..49c68f3d 100644 --- a/src/contexts/web/web.go +++ b/src/contexts/web/web.go @@ -85,6 +85,71 @@ type WEB struct { *ctx.Context } +var funcmap = template.FuncMap{"meta": func(arg ...interface{}) string { + if len(arg) == 0 { + return "" + } + + up := "" + if m, ok := arg[0].(*ctx.Message); ok { + if len(arg) == 1 { + return fmt.Sprintf("%v", m) + } else { + arg[0] = m.Meta + if key, ok := arg[1].(string); ok { + switch key { + case "context": + msg := m.Spawn(m.Target()) + msg.Cmd("context") + up = strings.Join(msg.Meta["result"], "") + } + } + } + } + + list := []string{} + switch data := arg[0].(type) { + case map[string][]string: + if len(arg) == 1 { + list = append(list, fmt.Sprintf("detail: %s\n", data["detail"])) + list = append(list, fmt.Sprintf("option: %s\n", data["option"])) + list = append(list, fmt.Sprintf("result: %s\n", data["result"])) + list = append(list, fmt.Sprintf("append: %s\n", data["append"])) + break + } + if key, ok := arg[1].(string); ok { + if list, ok = data[key]; ok { + arg = arg[1:] + } else { + return up + } + } else { + return fmt.Sprintf("%v", data) + } + case []string: + list = data + default: + if data == nil { + return "" + } + return fmt.Sprintf("%v", data) + } + + if len(arg) == 1 { + return strings.Join(list, "") + } + + index, ok := arg[1].(int) + if !ok { + return strings.Join(list, "") + } + + if index >= len(list) { + return "" + } + return list[index] +}} + func (web *WEB) generate(m *ctx.Message, uri string, arg ...string) string { // {{{ add, e := url.Parse(uri) m.Assert(e) @@ -678,7 +743,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", if !m.Options("right") { w.Header().Add("Content-Type", "text/html") - tmpl := template.Must(template.Must(template.ParseGlob(m.Conf("template_dir") + "/common/*.html")).ParseGlob(m.Conf("template_dir") + "upload.html")) + tmpl := template.Must(template.Must(template.New("fuck").Funcs(funcmap).ParseGlob(m.Conf("template_dir") + "/common/*.html")).ParseGlob(m.Conf("template_dir") + "upload.html")) m.Assert(tmpl) tmpl.ExecuteTemplate(w, "head", m.Meta) @@ -749,7 +814,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", } w.Header().Add("Content-Type", "text/html") - tmpl := template.Must(template.Must(template.ParseGlob(m.Conf("template_dir") + "/common/*.html")).ParseGlob(m.Conf("template_dir") + "upload.html")) + tmpl := template.Must(template.Must(template.New("fuck").Funcs(funcmap).ParseGlob(m.Conf("template_dir") + "/common/*.html")).ParseGlob(m.Conf("template_dir") + "upload.html")) m.Assert(tmpl) tmpl.ExecuteTemplate(w, "head", m.Meta) @@ -861,7 +926,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", } w.Header().Add("Content-Type", "text/html") - tmpl := template.Must(template.Must(template.ParseGlob(m.Conf("template_dir") + "/common/*.html")).ParseGlob(m.Conf("template_dir") + "travel.html")) + tmpl := template.Must(template.Must(template.New("fuck").Funcs(funcmap).ParseGlob(m.Conf("template_dir") + "/common/*.html")).ParseGlob(m.Conf("template_dir") + "travel.html")) m.Assert(tmpl) m.Assert(tmpl.ExecuteTemplate(w, "head", m.Meta)) @@ -913,10 +978,11 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", w := m.Data["response"].(http.ResponseWriter) w.Header().Add("Content-Type", "text/html") - tmpl := template.Must(template.ParseGlob(m.Conf("template_dir") + "/common/*.html")) + tmpl := template.Must(template.New("fuck").Funcs(funcmap).ParseGlob(m.Conf("template_dir") + "/common/*.html")) m.Assert(tmpl) - tmpl.ExecuteTemplate(w, "head", m.Meta) + m.Option("message", "hello") + tmpl.ExecuteTemplate(w, "head", m) tmpl.ExecuteTemplate(w, "tail", m.Meta) }}, }, diff --git a/usr/template/common/base.html b/usr/template/common/base.html index b1d1b79f..2f649b3f 100644 --- a/usr/template/common/base.html +++ b/usr/template/common/base.html @@ -2,7 +2,6 @@ - +
+{{meta .Meta}}
+{{meta . "context"}}
+
{{end}} {{define "message"}}
message -
{{index . "message"}}
+
{{meta . "message"}}
{{end}} diff --git a/usr/template/common/login.html b/usr/template/common/login.html index 31043e1c..1fda617e 100644 --- a/usr/template/common/login.html +++ b/usr/template/common/login.html @@ -1,7 +1,7 @@ {{define "login"}}
login
- +