本文整理汇总了Golang中github.com/justintan/wine.Context.TemplateHTML方法的典型用法代码示例。如果您正苦于以下问题:Golang Context.TemplateHTML方法的具体用法?Golang Context.TemplateHTML怎么用?Golang Context.TemplateHTML使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/justintan/wine.Context
的用法示例。
在下文中一共展示了Context.TemplateHTML方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: HandleIndex
func HandleIndex(c wine.Context) {
date := c.Params().GetStr("date")
_, e := time.Parse("2006-01-02", date)
if e != nil {
date = time.Now().In(time.FixedZone("", 8*3600)).Format("2006-01-02")
}
t, _ := time.Parse("2006-01-02", date)
previousDate := t.Add(-24 * 3600 * time.Second).In(time.FixedZone("", 8*3600)).Format("2006-01-02")
nextDate := t.Add(24 * 3600 * time.Second).In(time.FixedZone("", 8*3600)).Format("2006-01-02")
params := gox.M{}
params["previous_date"] = previousDate
params["next_date"] = nextDate
params["date"] = date
params["user_start_date"] = t.Add(-10 * 24 * 3600 * time.Second).In(time.FixedZone("", 8*3600)).Format("2006-01-02")
params["user_end_date"] = date
params["title"] = date + "(" + DaysOfWeek[t.Weekday()] + ")"
//log.Println(params)
resp, _ := http.Get("http://localhost:8001/daily-reports/" + date)
data, _ := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
var respMap map[string]interface{}
if err := json.Unmarshal(data, &respMap); err == nil {
if data, ok := respMap["data"].(map[string]interface{}); ok {
if reports, ok := data["reports"].([]interface{}); ok {
params["reports"] = reports
c.TemplateHTML("index.tmpl", params)
return
}
}
}
log.Println(respMap)
c.TemplateHTML("error.tmpl", nil)
return
}
示例2: HandleUserDailyReports
func HandleUserDailyReports(c wine.Context) {
start := c.Params().GetStr("start")
end := c.Params().GetStr("end")
var st time.Time
var et time.Time
var e error
if st, e = time.Parse("2006-01-02", start); e != nil {
c.TemplateHTML("error.tmpl", gox.M{"msg": "invalid date"})
return
}
if et, e = time.Parse("2006-01-02", end); e != nil {
c.TemplateHTML("error.tmpl", gox.M{"msg": "invalid date"})
return
}
userId, _ := strconv.ParseInt(c.Params().GetStr("user_id"), 10, 64)
if userId <= 0 {
c.TemplateHTML("error.tmpl", gox.M{"msg": "invalid date"})
return
}
resp, e := http.Get("http://localhost:8001/user/" + c.Params().GetStr("user_id") + "/daily-reports/" + start + "/" + end)
if e != nil {
c.TemplateHTML("error.tmpl", gox.M{"msg": "server error"})
return
}
data, _ := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
params := gox.M{}
previousStart := st.AddDate(0, 0, -10).Format("2006-01-02")
nextEnd := et.AddDate(0, 0, 10).Format("2006-01-02")
params["previous_start"] = previousStart
params["next_end"] = nextEnd
params["start"] = start
params["end"] = end
var respMap map[string]interface{}
if err := json.Unmarshal(data, &respMap); err == nil {
if data, ok := respMap["data"].(map[string]interface{}); ok {
if reports, ok := data["reports"].([]interface{}); ok {
for _, r := range reports {
m := r.(map[string]interface{})
t, _ := time.Parse("2006-01-02", m["date"].(string))
m["date"] = m["date"].(string) + "(" + DaysOfWeek[t.Weekday()] + ")"
}
if user, ok := data["user"].(map[string]interface{}); ok {
params["reports"] = reports
params["user"] = user
c.TemplateHTML("user_daily_reports.tmpl", params)
return
}
}
}
}
log.Println(respMap)
c.TemplateHTML("error.tmpl", gox.M{"msg": "server error"})
}