本文整理汇总了Golang中text/template.ExecuteTemplate函数的典型用法代码示例。如果您正苦于以下问题:Golang ExecuteTemplate函数的具体用法?Golang ExecuteTemplate怎么用?Golang ExecuteTemplate使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ExecuteTemplate函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: renderDefenseSelection
func renderDefenseSelection(w http.ResponseWriter, r *http.Request, errorMessage string) {
template := template.New("").Funcs(templateHelpers)
_, err := template.ParseFiles("templates/setup_defense_selection.html")
if err != nil {
handleWebErr(w, err)
return
}
matches, err := db.GetMatchesByType("elimination")
if err != nil {
handleWebErr(w, err)
return
}
var unplayedMatches []Match
for _, match := range matches {
if match.Status != "complete" {
unplayedMatches = append(unplayedMatches, match)
}
}
data := struct {
*EventSettings
Matches []Match
DefenseNames map[string]string
ErrorMessage string
}{eventSettings, unplayedMatches, defenseNames, errorMessage}
err = template.ExecuteTemplate(w, "setup_defense_selection.html", data)
if err != nil {
handleWebErr(w, err)
return
}
}
示例2: MatchReviewEditGetHandler
// Shows the page to edit the results for a match.
func MatchReviewEditGetHandler(w http.ResponseWriter, r *http.Request) {
if !UserIsAdmin(w, r) {
return
}
match, matchResult, _, err := getMatchResultFromRequest(r)
if err != nil {
handleWebErr(w, err)
return
}
template, err := template.ParseFiles("templates/edit_match_result.html", "templates/base.html")
if err != nil {
handleWebErr(w, err)
return
}
matchResultJson, err := matchResult.serialize()
if err != nil {
handleWebErr(w, err)
return
}
data := struct {
*EventSettings
Match *Match
MatchResultJson *MatchResultDb
}{eventSettings, match, matchResultJson}
err = template.ExecuteTemplate(w, "base", data)
if err != nil {
handleWebErr(w, err)
return
}
}
示例3: RefereeDisplayHandler
// Renders the referee interface for assigning fouls.
func RefereeDisplayHandler(w http.ResponseWriter, r *http.Request) {
template := template.New("").Funcs(templateHelpers)
_, err := template.ParseFiles("templates/referee_display.html")
if err != nil {
handleWebErr(w, err)
return
}
match := mainArena.currentMatch
matchType := match.CapitalizedType()
red1 := mainArena.AllianceStations["R1"].team
if red1 == nil {
red1 = &Team{}
}
red2 := mainArena.AllianceStations["R2"].team
if red2 == nil {
red2 = &Team{}
}
red3 := mainArena.AllianceStations["R3"].team
if red3 == nil {
red3 = &Team{}
}
blue1 := mainArena.AllianceStations["B1"].team
if blue1 == nil {
blue1 = &Team{}
}
blue2 := mainArena.AllianceStations["B2"].team
if blue2 == nil {
blue2 = &Team{}
}
blue3 := mainArena.AllianceStations["B3"].team
if blue3 == nil {
blue3 = &Team{}
}
data := struct {
*EventSettings
MatchType string
MatchDisplayName string
Red1 *Team
Red2 *Team
Red3 *Team
Blue1 *Team
Blue2 *Team
Blue3 *Team
RedFouls []Foul
BlueFouls []Foul
RedCards map[string]string
BlueCards map[string]string
Rules []string
EntryEnabled bool
}{eventSettings, matchType, match.DisplayName, red1, red2, red3, blue1, blue2, blue3,
mainArena.redRealtimeScore.Fouls, mainArena.blueRealtimeScore.Fouls, mainArena.redRealtimeScore.Cards,
mainArena.blueRealtimeScore.Cards, rules,
!(mainArena.redRealtimeScore.FoulsCommitted && mainArena.blueRealtimeScore.FoulsCommitted)}
err = template.ExecuteTemplate(w, "referee_display.html", data)
if err != nil {
handleWebErr(w, err)
return
}
}
示例4: ScoringDisplayHandler
// Renders the scoring interface which enables input of scores in real-time.
func ScoringDisplayHandler(w http.ResponseWriter, r *http.Request) {
if !UserIsAdmin(w, r) {
return
}
vars := mux.Vars(r)
alliance := vars["alliance"]
if alliance != "red" && alliance != "blue" {
handleWebErr(w, fmt.Errorf("Invalid alliance '%s'.", alliance))
return
}
template, err := template.ParseFiles("templates/scoring_display.html", "templates/base.html")
if err != nil {
handleWebErr(w, err)
return
}
data := struct {
*EventSettings
Alliance string
}{eventSettings, alliance}
err = template.ExecuteTemplate(w, "base", data)
if err != nil {
handleWebErr(w, err)
return
}
}
示例5: AllianceStationDisplayHandler
// Renders the team number and status display shown above each alliance station.
func AllianceStationDisplayHandler(w http.ResponseWriter, r *http.Request) {
if !UserIsReader(w, r) {
return
}
template := template.New("").Funcs(templateHelpers)
_, err := template.ParseFiles("templates/alliance_station_display.html")
if err != nil {
handleWebErr(w, err)
return
}
displayId := ""
if _, ok := r.URL.Query()["displayId"]; ok {
// Register the display in memory by its ID so that it can be configured to a certain station.
displayId = r.URL.Query()["displayId"][0]
}
data := struct {
*EventSettings
DisplayId string
}{eventSettings, displayId}
err = template.ExecuteTemplate(w, "alliance_station_display.html", data)
if err != nil {
handleWebErr(w, err)
return
}
}
示例6: executeTemplate
func executeTemplate(rootTemplte *template.Template, templateName string, machine datasource.Machine, hostAddr string) (string, error) {
template := rootTemplte.Lookup(templateName)
if template == nil {
return "", fmt.Errorf("template with name=%s wasn't found for root=%s",
templateName, rootTemplte)
}
buf := new(bytes.Buffer)
template.Funcs(map[string]interface{}{
"V": func(key string) string {
flag, err := machine.GetFlag(key)
if err != nil { // TODO excepts Not-Found
logging.Log(templatesDebugTag,
"Error while getting flag key=%s for machine=%s: %s",
key, machine.Name(), err)
return ""
}
return flag
},
"b64": func(text string) string {
return base64.StdEncoding.EncodeToString([]byte(text))
},
"b64template": func(templateName string) string {
text, err := executeTemplate(rootTemplte, templateName, machine, hostAddr)
if err != nil {
logging.Log(templatesDebugTag,
"Error while b64template for templateName=%s machine=%s: %s",
templateName, machine.Name(), err)
return ""
}
return base64.StdEncoding.EncodeToString([]byte(text))
},
})
ip, _ := machine.IP()
data := struct {
Mac string
IP string
Hostname string
Domain string
HostAddr string
}{
machine.Mac().String(),
ip.String(),
machine.Name(),
machine.Domain(),
hostAddr,
}
err := template.ExecuteTemplate(buf, templateName, &data)
if err != nil {
return "", err
}
str := buf.String()
str = strings.Trim(str, "\n")
return str, nil
}
示例7: MatchPlayHandler
// Shows the match play control interface.
func MatchPlayHandler(w http.ResponseWriter, r *http.Request) {
if !UserIsAdmin(w, r) {
return
}
practiceMatches, err := buildMatchPlayList("practice")
if err != nil {
handleWebErr(w, err)
return
}
qualificationMatches, err := buildMatchPlayList("qualification")
if err != nil {
handleWebErr(w, err)
return
}
eliminationMatches, err := buildMatchPlayList("elimination")
if err != nil {
handleWebErr(w, err)
return
}
template := template.New("").Funcs(templateHelpers)
_, err = template.ParseFiles("templates/match_play.html", "templates/base.html")
if err != nil {
handleWebErr(w, err)
return
}
matchesByType := map[string]MatchPlayList{"practice": practiceMatches,
"qualification": qualificationMatches, "elimination": eliminationMatches}
if currentMatchType == "" {
currentMatchType = "practice"
}
allowSubstitution := mainArena.currentMatch.Type != "qualification"
matchResult, err := db.GetMatchResultForMatch(mainArena.currentMatch.Id)
if err != nil {
handleWebErr(w, err)
return
}
isReplay := matchResult != nil
data := struct {
*EventSettings
MatchesByType map[string]MatchPlayList
CurrentMatchType string
Match *Match
AllowSubstitution bool
IsReplay bool
}{eventSettings, matchesByType, currentMatchType, mainArena.currentMatch, allowSubstitution, isReplay}
err = template.ExecuteTemplate(w, "base", data)
if err != nil {
handleWebErr(w, err)
return
}
}
示例8: main
func main() {
var pattern string
flag.StringVar(&pattern, "p", "templates/*.html", "template pattern like template/*.html")
template, err := template.ParseGlob(pattern)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
}
var mainTemplate string
flag.StringVar(&mainTemplate, "t", "index", "main template like index")
template.ExecuteTemplate(os.Stdout, mainTemplate, nil)
}
示例9: IndexHandler
// Serves the root page of Cheesy Arena.
func IndexHandler(w http.ResponseWriter, r *http.Request) {
template, err := template.ParseFiles("templates/index.html", "templates/base.html")
if err != nil {
handleWebErr(w, err)
return
}
data := struct {
*EventSettings
}{eventSettings}
err = template.ExecuteTemplate(w, "base", data)
if err != nil {
handleWebErr(w, err)
return
}
}
示例10: AnnouncerDisplayHandler
// Renders the announcer display which shows team info and scores for the current match.
func AnnouncerDisplayHandler(w http.ResponseWriter, r *http.Request) {
template := template.New("").Funcs(templateHelpers)
_, err := template.ParseFiles("templates/announcer_display.html", "templates/base.html")
if err != nil {
handleWebErr(w, err)
return
}
data := struct {
*EventSettings
}{eventSettings}
err = template.ExecuteTemplate(w, "base", data)
if err != nil {
handleWebErr(w, err)
return
}
}
示例11: MatchReviewHandler
// Shows the match review interface.
func MatchReviewHandler(w http.ResponseWriter, r *http.Request) {
if !UserIsReader(w, r) {
return
}
practiceMatches, err := buildMatchReviewList("practice")
if err != nil {
handleWebErr(w, err)
return
}
qualificationMatches, err := buildMatchReviewList("qualification")
if err != nil {
handleWebErr(w, err)
return
}
eliminationMatches, err := buildMatchReviewList("elimination")
if err != nil {
handleWebErr(w, err)
return
}
template, err := template.ParseFiles("templates/match_review.html", "templates/base.html")
if err != nil {
handleWebErr(w, err)
return
}
matchesByType := map[string][]MatchReviewListItem{"practice": practiceMatches,
"qualification": qualificationMatches, "elimination": eliminationMatches}
if currentMatchType == "" {
currentMatchType = "practice"
}
data := struct {
*EventSettings
MatchesByType map[string][]MatchReviewListItem
CurrentMatchType string
}{eventSettings, matchesByType, currentMatchType}
err = template.ExecuteTemplate(w, "base", data)
if err != nil {
handleWebErr(w, err)
return
}
}
示例12: AudienceDisplayHandler
// Renders the audience display to be chroma keyed over the video feed.
func AudienceDisplayHandler(w http.ResponseWriter, r *http.Request) {
if !UserIsReader(w, r) {
return
}
template := template.New("").Funcs(templateHelpers)
_, err := template.ParseFiles("templates/audience_display.html")
if err != nil {
handleWebErr(w, err)
return
}
data := struct {
*EventSettings
}{eventSettings}
err = template.ExecuteTemplate(w, "audience_display.html", data)
if err != nil {
handleWebErr(w, err)
return
}
}
示例13: FtaDisplayHandler
// Renders the FTA diagnostic display.
func FtaDisplayHandler(w http.ResponseWriter, r *http.Request) {
if !UserIsAdmin(w, r) {
return
}
// Retrieve the next few matches to show which defenses they will require.
numUpcomingMatches := 3
matches, err := db.GetMatchesByType(mainArena.currentMatch.Type)
if err != nil {
handleWebErr(w, err)
return
}
var upcomingMatches []Match
for _, match := range matches {
if match.Status != "complete" {
upcomingMatches = append(upcomingMatches, match)
if len(upcomingMatches) == numUpcomingMatches {
break
}
}
}
template := template.New("").Funcs(templateHelpers)
_, err = template.ParseFiles("templates/fta_display.html", "templates/base.html")
if err != nil {
handleWebErr(w, err)
return
}
data := struct {
*EventSettings
UpcomingMatches []Match
DefenseNames map[string]string
}{eventSettings, upcomingMatches, defenseNames}
err = template.ExecuteTemplate(w, "base", data)
if err != nil {
handleWebErr(w, err)
return
}
}
示例14: main
func main() {
flag.Parse()
template, err := template.ParseGlob(pattern)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
return
}
file, _ := os.Open(jsonFile)
buff := make([]byte, SIZE)
count, _ := file.Read(buff)
jsonData := buff[:count]
data := make(map[string]interface{})
if string(jsonData) != "" {
if err := json.Unmarshal(jsonData, &data); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
return
}
}
template.ExecuteTemplate(os.Stdout, mainTemplate, data)
}
示例15: executeTemplate
func executeTemplate(rootTemplte *template.Template, templateName string,
ds datasource.DataSource, machineInterface datasource.MachineInterface,
webServerAddr string) (string, error) {
template := rootTemplte.Lookup(templateName)
if template == nil {
return "", fmt.Errorf("template with name=%s wasn't found for root=%v",
templateName, rootTemplte)
}
mac := machineInterface.Mac().String()
buf := new(bytes.Buffer)
template.Funcs(map[string]interface{}{
"V": func(key string) string {
value, err := machineInterface.GetVariable(key)
if err != nil {
log.WithField("where", "templating.executeTemplate").WithError(err).Warn(
"error while GetVariable")
}
return value
},
"b64": func(text string) string {
return base64.StdEncoding.EncodeToString([]byte(text))
},
"b64template": func(templateName string) string {
text, err := executeTemplate(rootTemplte, templateName, ds, machineInterface, webServerAddr)
if err != nil {
log.WithField("where", "templating.executeTemplate").WithError(err).Warnf(
"error while executeTemplate(templateName=%s machine=%s)",
templateName, mac)
return ""
}
return base64.StdEncoding.EncodeToString([]byte(text))
},
})
etcdMembers, _ := ds.EtcdMembers()
machine, err := machineInterface.Machine(false, nil)
if err != nil {
return "", err
}
data := struct {
Mac string
IP string
Hostname string
Domain string
WebServerAddr string
EtcdEndpoints string
}{
mac,
machine.IP.String(),
machineInterface.Hostname(),
ds.ClusterName(),
webServerAddr,
etcdMembers,
}
err = template.ExecuteTemplate(buf, templateName, &data)
if err != nil {
return "", err
}
str := buf.String()
str = strings.Trim(str, "\n")
return str, nil
}