本文整理汇总了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)
}
示例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)
}
}
示例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)
}
}
示例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
}
示例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)
}
示例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)
}
}
示例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)
}
}
示例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)
}
}
示例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
}
示例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)
}
示例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()
}
示例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
}
示例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
}
示例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
}
示例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
}