当前位置: 首页>>代码示例>>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;未经允许,请勿转载。