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


Golang Template.ExecuteTemplate方法代碼示例

本文整理匯總了Golang中html/template.Template.ExecuteTemplate方法的典型用法代碼示例。如果您正苦於以下問題:Golang Template.ExecuteTemplate方法的具體用法?Golang Template.ExecuteTemplate怎麽用?Golang Template.ExecuteTemplate使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在html/template.Template的用法示例。


在下文中一共展示了Template.ExecuteTemplate方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: generateFile

// Generate the static HTML file for the post identified by the index.
func generateFile(td *PostData, idx bool) error {
	var w io.Writer

	// check if template exists
	tplName := td.D["Template"]
	var tpl *template.Template
	var ex bool

	if tpl, ex = postTpls[tplName]; !ex {
		return fmt.Errorf("Template not found: %s", tplName)
	}
	slug := td.D["Slug"]
	fw, err := os.Create(filepath.Join(PublicDir, slug))

	if err != nil {
		return fmt.Errorf("error creating static file %s: %s", slug, err)
	}
	defer fw.Close()

	// If this is the newest file, also save as index.html
	w = fw
	if idx {
		idxw, err := os.Create(filepath.Join(PublicDir, "index.html"))
		if err != nil {
			return fmt.Errorf("error creating static file index.html: %s", err)
		}
		defer idxw.Close()
		w = io.MultiWriter(fw, idxw)
	}
	return tpl.ExecuteTemplate(w, tplName+".amber", td)
}
開發者ID:juju2013,項目名稱:trofaf,代碼行數:32,代碼來源:gen.go

示例2: renderTemplate

func renderTemplate(w http.ResponseWriter, tName string, tType *template.Template, dataObj interface{}) {

	err := tType.ExecuteTemplate(w, tName+".html", dataObj)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}
開發者ID:patrickjr,項目名稱:poker2,代碼行數:7,代碼來源:users.go

示例3: BulkHandler

func BulkHandler(Layout *template.Template, Exits *Exits) func(http.ResponseWriter, *http.Request) {

	return func(w http.ResponseWriter, r *http.Request) {

		ip := r.URL.Query().Get("ip")
		if net.ParseIP(ip) == nil {
			if err := Layout.ExecuteTemplate(w, "bulk.html", nil); err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
			}
			return
		}

		port_str := r.URL.Query().Get("port")
		port, err := strconv.Atoi(port_str)
		port_str = "&port=" + port_str
		if err != nil {
			port = 80
			port_str = ""
		}

		str := fmt.Sprintf("# This is a list of all Tor exit nodes that can contact %s on Port %d #\n", ip, port)
		str += fmt.Sprintf("# You can update this list by visiting https://check.torproject.org/cgi-bin/TorBulkExitList.py?ip=%s%s #\n", ip, port_str)
		str += fmt.Sprintf("# This file was generated on %v #\n", Exits.UpdateTime.UTC().Format(time.UnixDate))
		str += Exits.Dump(ip, port)
		fmt.Fprintf(w, str)

	}

}
開發者ID:Ryman,項目名稱:check,代碼行數:29,代碼來源:handlers.go

示例4: Apply

func (h *Handler) Apply(w http.ResponseWriter, t *template.Template, tName string, app interface{}) (err error) {
	err = t.ExecuteTemplate(w, tName, app)
	if my, bad := mybad.Check(err, "template execution failure", "name", tName); bad {
		return my
	}
	return nil
}
開發者ID:sore0159,項目名稱:golang-helpers,代碼行數:7,代碼來源:handler.go

示例5: Render

// Render renders the doc to the given writer using the provided template.
func (d *Doc) Render(w io.Writer, t *template.Template) error {
	data := struct {
		*Doc
		Template *template.Template
	}{d, t}
	return t.ExecuteTemplate(w, "root", data)
}
開發者ID:jmptrader,項目名稱:mdpresent,代碼行數:8,代碼來源:parse.go

示例6: RegistroPrendaGET

func RegistroPrendaGET(html *template.Template) gin.HandlerFunc {
	return func(c *gin.Context) {
		mapa := MapaInfo{}
		mapa.ObtenerDatosRegistroPrenda()
		html.ExecuteTemplate(c.Writer, "registroPrenda.html", mapa)
	}
}
開發者ID:gophergala2016,項目名稱:kentia,代碼行數:7,代碼來源:prenda.go

示例7: main

func main() {
	var err error
	var tpl *template.Template
	tpl, err = tpl.ParseGlob("templates/*.gohtml")
	if err != nil {
		log.Fatalln(err)
	}

	err = tpl.Execute(os.Stdout, Page{
		Title: "My Title 2",
		Body:  "hello world",
	})
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println("\n***************")

	err = tpl.ExecuteTemplate(os.Stdout, "tpl.gohtml", Page{
		Title: "My Title 2",
		Body:  "hello world",
	})
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println("\n***************")

	err = tpl.ExecuteTemplate(os.Stdout, "tpl2.gohtml", Page{
		Title: "My Title 2",
		Body:  "hello world",
	})
	if err != nil {
		log.Fatalln(err)
	}
}
開發者ID:RobertoSuarez,項目名稱:GolangTraining,代碼行數:34,代碼來源:main.go

示例8: index

// the index page of the chat
func index(indexTemplate *template.Template, w http.ResponseWriter, r *http.Request) {
	//disable caching: HTTP/1.1 HTTP/1.0 and proxies
	//from http://stackoverflow.com/questions/49547/making-sure-a-web-page-is-not-cached-across-all-browsers
	w.Header().Set("Cache-Control", "no-cache,no-store,must-revalidate")
	w.Header().Set("Pragma:", "no-cache")
	w.Header().Set("Expires", "0")
	session, err := sessionStorage.GetSession(w, r)
	if err != nil {
		log.Fatal(err)
	}
	if r.Method == "POST" {
		user := r.FormValue("username")
		session.PutVal("user.name", user)
	}
	_, err = session.GetVal("user.name")
	if err != nil {
		err = indexTemplate.ExecuteTemplate(w, "signup", nil)
		if err != nil {
			log.Fatal(err)
		}
		return
	}
	defaultRoom.mutex.Lock()
	defer defaultRoom.mutex.Unlock()
	err = indexTemplate.ExecuteTemplate(w, "pull_chat", defaultRoom)
	if err != nil {
		log.Fatal(err)
	}
}
開發者ID:hanst99,項目名稱:go-chatapp,代碼行數:30,代碼來源:chat.go

示例9: Render

func (ts *TemplateStore) Render(w http.ResponseWriter, name string, data Model) {
	var tmpl *template.Template
	if ts.Development {
		includes, err := filepath.Glob(ts.TemplateDir + "includes/*.tmpl")
		if err != nil {
			log.Fatal(err)
		}
		files := append(includes, ts.TemplateDir+"layouts/"+name)
		tmpl = template.Must(template.New("func").Funcs(ts.funcs).ParseFiles(files...))
	} else {
		var ok bool
		ts.RLock()
		tmpl, ok = ts.templates[name]
		ts.RUnlock()
		if !ok {
			http.Error(w, "404. Page not found", 404)
			return
		}
	}

	buf := ts.bufpool.get()
	defer ts.bufpool.reset(buf)

	err := tmpl.ExecuteTemplate(buf, "base", data)
	if err != nil {
		log.Println(err)
		http.Error(w, "500. Internal server error", 500)
		return
	}

	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	buf.WriteTo(w)
	return
}
開發者ID:cagnosolutions,項目名稱:webc,代碼行數:34,代碼來源:tmpl.go

示例10: respondWithTemplate

func respondWithTemplate(
	templates *template.Template, templateName string, templateContext interface{}, w http.ResponseWriter,
	r *http.Request) error {

	// you access the cached templates with the defined name, not the filename
	return templates.ExecuteTemplate(w, templateName, templateContext)
}
開發者ID:onlyafly,項目名稱:galapagia,代碼行數:7,代碼來源:responses.go

示例11: pageTitle

// pageTitle executes "title" template and returns its result or defaultTitle.
func pageTitle(t *html.Template) string {
	b := new(bytes.Buffer)
	if err := t.ExecuteTemplate(b, "title", nil); err != nil || b.Len() == 0 {
		return defaultTitle
	}
	return b.String()
}
開發者ID:pathikdevani,項目名稱:ioweb2015,代碼行數:8,代碼來源:template.go

示例12: RenderDetail

func RenderDetail(shdarray []*DP, t *template.Template, sh string) error {
	var hisName = time.Now().Format("2006-01-02")
	dshfilename := homeDir + "/changshi/today/detail_" + sh + ".html"
	if _, err := os.Stat(dshfilename); os.IsExist(err) {
		os.Remove(dshfilename)
	}
	dshf, _ := os.Create(dshfilename)
	dshfbw := bufio.NewWriter(dshf)
	err := t.ExecuteTemplate(dshfbw, "detail.tmpl", shdarray)
	if err != nil {
		return err
	}
	dshfbw.Flush()
	dshf.Close()

	dshhfilename := homeDir + "/changshi/his/d_" + sh + "_" + hisName + ".html"
	if _, err := os.Stat(dshhfilename); os.IsExist(err) {
		os.Remove(dshhfilename)
	}
	dshhf, _ := os.Create(dshhfilename)
	dshhfbw := bufio.NewWriter(dshhf)
	err1 := t.ExecuteTemplate(dshhfbw, "detail.tmpl", shdarray)
	if err1 != nil {
		return err
	}
	dshhfbw.Flush()
	dshhf.Close()
	return nil
}
開發者ID:wenxiaoyu,項目名稱:traffic,代碼行數:29,代碼來源:main.go

示例13: build_post

/*
Builds a post based on the template
*/
func build_post(ps Post, ptype string) string {
	var doc bytes.Buffer
	var body, name string
	var err error
	var tml *template.Template
	if ptype == "post" {
		tml, err = template.ParseFiles("./templates/post.html", "./templates/base.html")
		name = "./output/posts/" + ps.Slug + ".html"
	} else {
		// This should read the pages template
		tml, err = template.ParseFiles("./templates/page.html", "./templates/base.html")
		name = "./output/pages/" + ps.Slug + ".html"
	}
	err = tml.ExecuteTemplate(&doc, "base", ps)
	if err != nil {
		fmt.Println("Error executing template: ", err)
	}
	body = doc.String()

	f, err := os.Create(name)
	defer f.Close()
	n, err := io.WriteString(f, body)

	if err != nil {
		fmt.Println("Error while writing output: ", n, err)
	}

	return body
}
開發者ID:kushaldas,項目名稱:shonku,代碼行數:32,代碼來源:scrdkd.go

示例14: elemFn

func elemFn(t *htemp.Template, e present.Elem) (htemp.HTML, error) {
	var buf bytes.Buffer
	if err := t.ExecuteTemplate(&buf, e.TemplateName(), e); err != nil {
		return "", err
	}
	return htemp.HTML(buf.Bytes()), nil
}
開發者ID:sbryant,項目名稱:gopkgdoc,代碼行數:7,代碼來源:template.go

示例15: executeTemplate

func executeTemplate(t *template.Template, p Page) ([]byte, error) {
	var out bytes.Buffer
	err := t.ExecuteTemplate(&out, "layout", p)
	if err != nil {
		return []byte{}, err
	}
	return out.Bytes(), nil
}
開發者ID:kiasaki,項目名稱:marks,代碼行數:8,代碼來源:templates.go


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