當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Context.TemplateHTML方法代碼示例

本文整理匯總了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
}
開發者ID:justintan,項目名稱:work_report_web,代碼行數:38,代碼來源:index.go

示例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"})
}
開發者ID:justintan,項目名稱:work_report_web,代碼行數:63,代碼來源:user_daily_reports.go


注:本文中的github.com/justintan/wine.Context.TemplateHTML方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。