1
0
forked from x/ContextOS

mac add funcmap

This commit is contained in:
shaoying 2018-05-31 09:56:37 +08:00
parent 5348baeb2b
commit 8fa1cfe077
3 changed files with 77 additions and 8 deletions

View File

@ -85,6 +85,71 @@ type WEB struct {
*ctx.Context *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 { // {{{ func (web *WEB) generate(m *ctx.Message, uri string, arg ...string) string { // {{{
add, e := url.Parse(uri) add, e := url.Parse(uri)
m.Assert(e) m.Assert(e)
@ -678,7 +743,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
if !m.Options("right") { if !m.Options("right") {
w.Header().Add("Content-Type", "text/html") 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) m.Assert(tmpl)
tmpl.ExecuteTemplate(w, "head", m.Meta) tmpl.ExecuteTemplate(w, "head", m.Meta)
@ -749,7 +814,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
} }
w.Header().Add("Content-Type", "text/html") 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) m.Assert(tmpl)
tmpl.ExecuteTemplate(w, "head", m.Meta) tmpl.ExecuteTemplate(w, "head", m.Meta)
@ -861,7 +926,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
} }
w.Header().Add("Content-Type", "text/html") 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)
m.Assert(tmpl.ExecuteTemplate(w, "head", m.Meta)) 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 := m.Data["response"].(http.ResponseWriter)
w.Header().Add("Content-Type", "text/html") 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) m.Assert(tmpl)
tmpl.ExecuteTemplate(w, "head", m.Meta) m.Option("message", "hello")
tmpl.ExecuteTemplate(w, "head", m)
tmpl.ExecuteTemplate(w, "tail", m.Meta) tmpl.ExecuteTemplate(w, "tail", m.Meta)
}}, }},
}, },

View File

@ -2,7 +2,6 @@
<!DOCTYPE html> <!DOCTYPE html>
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<link rel="icon" href="favicon.ico" type="image/x-icon"/>
<style> <style>
legend { legend {
font-size:16px; font-size:16px;
@ -21,11 +20,15 @@
</style> </style>
</head> </head>
<body> <body>
<pre>
{{meta .Meta}}<br/>
{{meta . "context"}}<br/>
</pre>
{{end}} {{end}}
{{define "message"}} {{define "message"}}
<fieldset class="message"><legend>message</legend> <fieldset class="message"><legend>message</legend>
<pre>{{index . "message"}}</pre> <pre>{{meta . "message"}}</pre>
</fieldset> </fieldset>
{{end}} {{end}}

View File

@ -1,7 +1,7 @@
{{define "login"}} {{define "login"}}
<fieldset><legend>login</legend> <fieldset><legend>login</legend>
<form method="POST" action="/login" onsubmit="return login(event)"> <form method="POST" action="/login" onsubmit="return login(event)">
<label>username:</label><input type="text" name="username" value="{{index .username 0}}" required autofocus> <label>username:</label><input type="text" name="username" value="{{meta .username}}" required autofocus>
<label>password:</label><input type="password" name="password" required> <label>password:</label><input type="password" name="password" required>
<input type="submit"> <input type="submit">
</form> </form>