本文整理汇总了Golang中html/template.ParseFiles函数的典型用法代码示例。如果您正苦于以下问题:Golang ParseFiles函数的具体用法?Golang ParseFiles怎么用?Golang ParseFiles使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ParseFiles函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: RenderTemplate
func RenderTemplate(w http.ResponseWriter, tmpl string, arg interface{}) {
skel, err := template.ParseFiles(fmt.Sprintf("%s/skeleton.html", templatesDir))
if err != nil {
log.Println("Error when parsing a template: %s", err)
return
}
content, err := template.ParseFiles(fmt.Sprintf("%s/%s.html", templatesDir, tmpl))
if err != nil {
log.Println("Error when parsing a template: %s", err)
return
}
buff := &bytes.Buffer{}
err = content.Execute(buff, arg)
if err != nil {
log.Println("Error when executing a template: %s", err)
return
}
c := template.HTML(buff.String())
page := &Page{Content: c}
err = skel.Execute(w, page)
if err != nil {
log.Println("Error when executing a template: %s", err)
return
}
}
开发者ID:acieroid,项目名称:goeland,代码行数:28,代码来源:goeland.go
示例2: errorHandler
// Handle errors here, this allows us to control the format of the output rather
// than using http.Error() defaults
func errorHandler(w http.ResponseWriter, r *http.Request, status int, err string) {
w.WriteHeader(status)
switch status {
case http.StatusNotFound:
logHandler("ERROR", fmt.Sprintf("client %s tried to request %v", r.RemoteAddr, r.URL.Path))
page := template.Must(template.ParseFiles(
"static/_base.html",
"static/404.html",
))
if err := page.Execute(w, nil); err != nil {
errorHandler(w, r, http.StatusInternalServerError, err.Error())
return
}
case http.StatusInternalServerError:
logHandler("ERROR", fmt.Sprintf("an internal server error occured when %s requested %s with error:\n%s", r.RemoteAddr, r.URL.Path, err))
page := template.Must(template.ParseFiles(
"static/_base.html",
"static/500.html",
))
if err := page.Execute(w, nil); err != nil {
// IF for some reason the tempalets for 500 errors fails, fallback
// on http.Error()
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}
开发者ID:GDG-Gigcity,项目名称:gigcity-site,代码行数:31,代码来源:gigcity.go
示例3: Login
// Login template
func (ctrl *Access) Login(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
t := template.Must(template.ParseFiles("views/layouts/application.htm", "views/login/login.htm"))
if err := t.Execute(w, &Access{}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
} else {
r.ParseForm()
user := ctrl.Auth(r.Form["username"][0], r.Form["password"][0])
if user != nil {
t := template.Must(template.ParseFiles("views/layouts/application.htm", "views/login/info.htm"))
page := &Access{LoginPage: &LoginPage{UserName: r.Form["username"][0], HasVideoAccess: user.HasVideoAccess}}
if err := t.Execute(w, page); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
} else {
http.Error(w, "Auth", http.StatusForbidden)
}
}
}
开发者ID:droff,项目名称:polaris,代码行数:27,代码来源:access.go
示例4: excute
func (this *Pages) excute() (err error) {
buffer := bytes.NewBufferString("")
if this.NotFoundPath == "" {
defaultTemplate.Execute(buffer, struct{ Body string }{not_found_body})
} else {
notFoundTemplate, err := template.ParseFiles(this.NotFoundPath)
if err != nil {
return err
}
notFoundTemplate.Execute(buffer, nil)
}
this.notFound = buffer.String()
buffer = bytes.NewBufferString("")
if this.InternalErrorPath == "" {
defaultTemplate.Execute(buffer, struct{ Body string }{internal_error_body})
} else {
internalErrorTemplate, err := template.ParseFiles(this.InternalErrorPath)
if err != nil {
return err
}
internalErrorTemplate.Execute(buffer, nil)
}
this.internalError = buffer.String()
return
}
开发者ID:kobeld,项目名称:gorecover,代码行数:27,代码来源:gorecover.go
示例5: treeOperations
func treeOperations(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
err := r.ParseForm()
if err != nil {
io.WriteString(w, fmt.Sprintf("Error parsing the submitted form:\n%s", err))
}
var v int
v, err = strconv.Atoi(r.Form["number"][0])
if err != nil {
io.WriteString(w, fmt.Sprintf("Error parsing the given number:\n%s", err))
}
if r.Form["insert"] != nil {
fmt.Printf("\nInserting [%d]\n", v)
btree = Insert(btree, v)
} else if r.Form["delete"] != nil {
fmt.Printf("\nDeleting [%d]\n", v)
btree = Delete(btree, v)
} else {
io.WriteString(w, "Neither an insert request, nor a delete request")
return
}
renderer := PrintbTree(btree, r.Form["numbers"][0])
err =
template.Must(template.ParseFiles("treedisplay.html")).Execute(w, &renderer)
if err != nil {
io.WriteString(w, fmt.Sprintf("Error generating HTML file from the template:\n%s", err))
return
}
} else {
/* The next if loop is a hack to avoid re-initialization due to
* the GET request that will come when the page gets rendered
* during the response of the POST (the above block) */
if btree == nil {
btree, _ = InitializebTree(3)
fmt.Println("Initializing the btree")
for _, v := range []int{6, 1, 3, 10, 4, 7, 8, 9, 18, 12, 13,
19, 15, 22, 33, 35, 44, 70, 37, 38, 39, 50, 60, 55, 80,
90, 101, 102, 100, 110, 120, 57, 58} {
btree = Insert(btree, v)
}
renderer := PrintbTree(btree, "")
err :=
template.Must(template.ParseFiles("treedisplay.html")).Execute(w, &renderer)
if err != nil {
io.WriteString(w, fmt.Sprintf("Error generating HTML file from the template:\n%s", err))
return
}
}
}
}
开发者ID:psankar,项目名称:btree-go,代码行数:60,代码来源:btree.go
最后编辑: kuteng 文档更新时间: 2021-08-23 19:14 作者:kuteng