当前位置: 首页>>代码示例>>Golang>>正文


Golang template.ExecuteTemplate函数代码示例

本文整理汇总了Golang中html/template.ExecuteTemplate函数的典型用法代码示例。如果您正苦于以下问题:Golang ExecuteTemplate函数的具体用法?Golang ExecuteTemplate怎么用?Golang ExecuteTemplate使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了ExecuteTemplate函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: SponsorSlidesGetHandler

// Shows the sponsor slides configuration page.
func SponsorSlidesGetHandler(w http.ResponseWriter, r *http.Request) {
	if !UserIsAdmin(w, r) {
		return
	}

	template, err := template.ParseFiles("templates/setup_sponsor_slides.html", "templates/base.html")
	if err != nil {
		handleWebErr(w, err)
		return
	}
	sponsorSlides, err := db.GetAllSponsorSlides()
	if err != nil {
		handleWebErr(w, err)
		return
	}
	data := struct {
		*EventSettings
		SponsorSlides []SponsorSlide
	}{eventSettings, sponsorSlides}
	err = template.ExecuteTemplate(w, "base", data)
	if err != nil {
		handleWebErr(w, err)
		return
	}
}
开发者ID:fangeugene,项目名称:cheesy-arena,代码行数:26,代码来源:setup_sponsor_slides.go

示例2: Publish

func (h *HTMLRedirectAlias) Publish(path string, permalink string, page interface{}) (err error) {
	if path, err = h.Translate(path); err != nil {
		jww.ERROR.Printf("%s, skipping.", err)
		return nil
	}

	t := "alias"
	if strings.HasSuffix(path, ".xhtml") {
		t = "alias-xhtml"
	}

	template := defaultAliasTemplates
	if h.Templates != nil {
		template = h.Templates
		t = "alias.html"
	}

	buffer := new(bytes.Buffer)
	err = template.ExecuteTemplate(buffer, t, &AliasNode{permalink, page})
	if err != nil {
		return
	}

	return helpers.WriteToDisk(path, buffer, hugofs.Destination())
}
开发者ID:digitalcraftsman,项目名称:hugo,代码行数:25,代码来源:htmlredirect.go

示例3: defaultFormatFunc

func defaultFormatFunc(h http.HTTP) http.Formatter {

	var bt []byte
	var err error

	hc := h.(*HipChat)
	b := new(bytes.Buffer)
	template := hc.Template()

	body := new(Body)
	body.Notify = true

	return func(e *log.Entry) []byte {

		bt = bt[0:0]
		b.Reset()
		body.From = e.ApplicationID
		body.Color = hc.GetDisplayColor(e.Level)

		if err = template.ExecuteTemplate(b, "hipchat", e); err != nil {
			log.WithFields(log.F("error", err)).Error("Error parsing HipChat handler template")
		}

		body.Message = b.String()

		// shouldn't be possible to fail here
		// at least with the default handler...
		bt, _ = json.Marshal(body)

		return bt
	}
}
开发者ID:go-playground,项目名称:log,代码行数:32,代码来源:hipchat.go

示例4: LowerThirdsGetHandler

// Shows the lower third configuration page.
func LowerThirdsGetHandler(w http.ResponseWriter, r *http.Request) {
	if !UserIsAdmin(w, r) {
		return
	}

	template, err := template.ParseFiles("templates/setup_lower_thirds.html", "templates/base.html")
	if err != nil {
		handleWebErr(w, err)
		return
	}
	lowerThirds, err := db.GetAllLowerThirds()
	if err != nil {
		handleWebErr(w, err)
		return
	}
	data := struct {
		*EventSettings
		LowerThirds []LowerThird
	}{eventSettings, lowerThirds}
	err = template.ExecuteTemplate(w, "base", data)
	if err != nil {
		handleWebErr(w, err)
		return
	}
}
开发者ID:fangeugene,项目名称:cheesy-arena,代码行数:26,代码来源:setup_lower_thirds.go

示例5: TeamEditGetHandler

// Shows the page to edit a team's fields.
func TeamEditGetHandler(w http.ResponseWriter, r *http.Request) {
	if !UserIsAdmin(w, r) {
		return
	}

	vars := mux.Vars(r)
	teamId, _ := strconv.Atoi(vars["id"])
	team, err := db.GetTeamById(teamId)
	if err != nil {
		handleWebErr(w, err)
		return
	}
	if team == nil {
		http.Error(w, fmt.Sprintf("Error: No such team: %d", teamId), 400)
		return
	}

	template, err := template.ParseFiles("templates/edit_team.html", "templates/base.html")
	if err != nil {
		handleWebErr(w, err)
		return
	}
	data := struct {
		*EventSettings
		*Team
	}{eventSettings, team}
	err = template.ExecuteTemplate(w, "base", data)
	if err != nil {
		handleWebErr(w, err)
		return
	}
}
开发者ID:fangeugene,项目名称:cheesy-arena,代码行数:33,代码来源:setup_teams.go

示例6: renderSchedule

func renderSchedule(w http.ResponseWriter, r *http.Request, errorMessage string) {
	teams, err := db.GetAllTeams()
	if err != nil {
		handleWebErr(w, err)
		return
	}
	template, err := template.ParseFiles("templates/setup_schedule.html", "templates/base.html")
	if err != nil {
		handleWebErr(w, err)
		return
	}
	data := struct {
		*EventSettings
		MatchType        string
		ScheduleBlocks   []ScheduleBlock
		NumTeams         int
		Matches          []Match
		TeamFirstMatches map[int]string
		ErrorMessage     string
	}{eventSettings, cachedMatchType, cachedScheduleBlocks, len(teams), cachedMatches, cachedTeamFirstMatches,
		errorMessage}
	err = template.ExecuteTemplate(w, "base", data)
	if err != nil {
		handleWebErr(w, err)
		return
	}
}
开发者ID:agdespopoulos,项目名称:cheesy-arena,代码行数:27,代码来源:setup_schedule.go

示例7: defaultFormatFunc

func defaultFormatFunc(email *Email) Formatter {
	var err error
	b := new(bytes.Buffer)

	// apparently there is a race condition when I was using
	// email.to... below in the SetHeader for whatever reason
	// so copying the "to" values solves the issue
	// I wonder if it's a flase positive in the race detector.....
	to := make([]string, len(email.to), len(email.to))
	copy(to, email.to)

	template := email.Template()
	message := gomail.NewMessage()

	message.SetHeader("From", email.from)
	message.SetHeader("To", to...)

	return func(e *log.Entry) *gomail.Message {
		b.Reset()
		if err = template.ExecuteTemplate(b, "email", e); err != nil {
			log.WithFields(log.F("error", err)).Error("Error parsing Email handler template")
		}

		message.SetHeader("Subject", e.Message)
		message.SetBody(contentType, b.String())

		return message
	}
}
开发者ID:go-playground,项目名称:log,代码行数:29,代码来源:email.go

示例8: statusResponse

func statusResponse(res http.ResponseWriter, req *http.Request) {
	fmt.Fprintf(res, "<!doctype html>\n")
	fmt.Fprintf(res, "<html>\n")
	fmt.Fprintf(res, "<body>\n")

	lockfile := getLockfile()

	switch req.Method {
	case "POST":
		switch req.PostFormValue("operation") {
		case "lock":
			if lockfile.IsLocked {
				fmt.Fprintf(res, "cannot lock, sorry<br>\n")
			} else {
				locker := req.PostFormValue("locker")
				if isValidLocker(locker) {
					lockfile.IsLocked = true
					lockfile.HeldBy = req.PostFormValue("locker")
					lockfile.ModificationTime = currentTimeString()
					writeLockfile(lockfile)
				} else {
					fmt.Fprintf(res, "you have terrible taste in names<br>\n")
				}
			}
		case "release":
			lockfile.IsLocked = false
			lockfile.ModificationTime = currentTimeString()
			writeLockfile(lockfile)
		}
	}

	template, err := template.New("locked_greeting").Parse(
		`{{define "LockStatusPage"}}
			{{if .IsLocked}}
				<b>{{.HeldBy}}</b> has the lock since <b>{{.ModificationTime}}</b><br>
				<form method='POST'>
					<input type='hidden' name='operation' value='release'>
					<input type='submit' value='release lock'>
				</form>
			{{else}}
				unlocked right now, since <b>{{.ModificationTime}}</b><br>
				<form method='POST'>
					<input type='hidden' name='operation' value='lock'>
					<input type='text' name='locker' placeholder='My Glorious Name'>
					<input type='submit' value='grab lock'>
				</form>
			{{end}}
		{{end}}`)
	if err != nil {
		panic(err)
	}
	if err := template.ExecuteTemplate(res, "LockStatusPage", lockfile); err != nil {
		panic(err)
	}
	fmt.Fprintf(res, "</body>\n")
	fmt.Fprintf(res, "</html>\n")
}
开发者ID:MichalPokorny,项目名称:builderlock,代码行数:57,代码来源:web.go

示例9: writeIndex

func (site Site) writeIndex() {
	indexFile := fmt.Sprintf("%s/index.html", site.OutputPath)

	template := template.Must(template.ParseFiles("templates/index.html", "templates/base.html"))

	file, err := os.Create(indexFile)
	if err != nil {
		log.Panic(err)
	}
	template.ExecuteTemplate(file, "base", site)
}
开发者ID:valgreens,项目名称:go-polo,代码行数:11,代码来源:writer.go

示例10: writeArticle

func (site Site) writeArticle(article parser.ParsedFile) {
	filePath := fmt.Sprintf("%s/%s.html", site.OutputPath, article.Slug)

	template := template.Must(template.ParseFiles("templates/article.html", "templates/base.html"))

	file, err := os.Create(filePath)
	if err != nil {
		log.Panic(err)
	}
	site.Article = article
	template.ExecuteTemplate(file, "base", site)
}
开发者ID:valgreens,项目名称:go-polo,代码行数:12,代码来源:writer.go

示例11: renderSettings

func renderSettings(w http.ResponseWriter, r *http.Request, errorMessage string) {
	template, err := template.ParseFiles("templates/setup_settings.html", "templates/base.html")
	if err != nil {
		handleWebErr(w, err)
		return
	}
	data := struct {
		*EventSettings
		ErrorMessage string
	}{eventSettings, errorMessage}
	err = template.ExecuteTemplate(w, "base", data)
	if err != nil {
		handleWebErr(w, err)
		return
	}
}
开发者ID:Team254,项目名称:cheesy-arena,代码行数:16,代码来源:setup_settings.go

示例12: Render

func (tm *TemplateManager) Render(templateName string, data interface{}) (string, error) {
	template, ok := tm.templates[templateName]
	if !ok {
		return "", errors.New("Template '" + templateName + "' does not exist.")
	}

	buf := tm.bufpool.Get()
	defer tm.bufpool.Put(buf)

	err := template.ExecuteTemplate(buf, "root", data)
	if err != nil {
		return "", err
	}

	return buf.String(), nil
}
开发者ID:xrstf,项目名称:raziel,代码行数:16,代码来源:templates.go

示例13: FieldGetHandler

// Shows the field configuration page.
func FieldGetHandler(w http.ResponseWriter, r *http.Request) {
	template, err := template.ParseFiles("templates/setup_field.html", "templates/base.html")
	if err != nil {
		handleWebErr(w, err)
		return
	}
	data := struct {
		*EventSettings
		AllianceStationDisplays map[string]string
		LightsMode              string
	}{eventSettings, mainArena.allianceStationDisplays, mainArena.lights.currentMode}
	err = template.ExecuteTemplate(w, "base", data)
	if err != nil {
		handleWebErr(w, err)
		return
	}
}
开发者ID:agdespopoulos,项目名称:cheesy-arena,代码行数:18,代码来源:setup_field.go

示例14: main

func main() {

	// definiujemy templatki które mają dziedziczyć z base.html
	// parse files weżmie pod uwagę przekazane parametry
	templates := templateMap{
		"index.html": template.Must(template.ParseFiles("view/index.html", "view/base.html")),
		"other.html": template.Must(template.ParseFiles("view/other.html", "view/base.html")),
	}

	// przekazujemy trochę danych do template'a
	// cała mapa będzie dostępna jako "."
	data := map[string]string{"name": "John"}

	// renderujemy templatki
	for _, template := range templates {
		template.ExecuteTemplate(os.Stdout, "base", data)
	}
}
开发者ID:exu,项目名称:go-workshops,代码行数:18,代码来源:40-pseudoinheritance.go

示例15: writePage

func (site Site) writePage(page parser.ParsedFile) {
	// First of all create the pages/ folder if it doesn't exist
	pagesPath := fmt.Sprintf("%s/pages", site.OutputPath)
	if _, err := os.Stat(pagesPath); os.IsNotExist(err) {
		os.Mkdir(pagesPath, 0777)
	}

	filePath := fmt.Sprintf("%s/%s.html", pagesPath, page.Slug)

	template := template.Must(template.ParseFiles("templates/page.html", "templates/base.html"))

	file, err := os.Create(filePath)
	if err != nil {
		log.Panic(err)
	}
	site.Page = page
	template.ExecuteTemplate(file, "base", site)
}
开发者ID:valgreens,项目名称:go-polo,代码行数:18,代码来源:writer.go


注:本文中的html/template.ExecuteTemplate函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。