本文整理汇总了Golang中template.MustParseFile函数的典型用法代码示例。如果您正苦于以下问题:Golang MustParseFile函数的具体用法?Golang MustParseFile怎么用?Golang MustParseFile使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了MustParseFile函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: init
func init() {
for _, tmpl := range []string{"index", "404", "edit"} {
templates[tmpl] = template.MustParseFile("/var/www/apps/gophertimes/templates/"+tmpl+".html", nil)
}
templates["all.rss"] = template.MustParseFile("/var/www/apps/gophertimes/templates/all.rss", nil)
/* argv := []string{
"6g",
"-V",
"/dev/null",
"EOF",
}
exe, err := exec.LookPath(argv[0])
if err != nil {
fmt.Println("run:", err)
}
cmd, err := exec.Run(exe, argv, nil, "", exec.DevNull, exec.Pipe, exec.DevNull)
if err != nil {
fmt.Println("run:", err)
}
buf, err := ioutil.ReadAll(cmd.Stdout)
*/
goVersion = "release.r57.1 8294"
}
示例2: readFixture
func readFixture(filename string, data interface{}) string {
buf := bytes.NewBufferString("")
template.MustParseFile(fmt.Sprintf("fixture/%s.xml.gt", filename), nil).Execute(buf, data)
return buf.String()
}
示例3: getTemplate
// Cache templates
func getTemplate(name string) *template.Template {
tmp := server.Templates[name]
if tmp == nil {
tmp = template.MustParseFile(getTemplateName(name), filters)
server.Templates[name] = tmp
}
return tmp
}
示例4: init
func init() {
/*cache templates*/
for _, tmpl := range []string{"index", "list", "show"} {
templates[tmpl] = template.MustParseFile(tmpl+".html", nil)
}
/*setup handlers*/
http.HandleFunc("/", makeHandler(index))
http.HandleFunc("/upload", makeHandler(upload))
http.HandleFunc("/show/", makeHandler(show))
http.HandleFunc("/img", makeHandler(img))
http.HandleFunc("/list", makeHandler(list))
}
示例5: ParseTemplate
// Given a filename and dictionary context, create a context dict+("file"=>filename),
// and read a template specified by relpath. See GetTemplatePath().
func ParseTemplate(filename string, dict map[string]string, relpath []string) (string, os.Error) {
var tpath = GetTemplatePath(relpath)
if tpath == "" {
return "", NoTemplateError
}
if DEBUG && DEBUG_LEVEL > 0 {
log.Printf("scanning: %s", tpath)
if DEBUG_LEVEL > 1 {
log.Printf("context:\n%v", dict)
}
}
var template = template.MustParseFile(tpath, nil)
var buff = bytes.NewBuffer(make([]byte, 0, 1<<20))
var errTExec = template.Execute(buff, combinedData(dict, extraData(filename)))
return buff.String(), errTExec
//return mustache.RenderFile(tpath, dict, map[string]string{"file":filename, "test":TestName(filename)}), nil
}
示例6: init
package main
import (
"container/vector"
"ext/direct"
"ext/jsb2"
"fmt"
"http"
"io/ioutil"
"log"
"os"
"template"
)
var (
TEMPLATE = template.MustParseFile("example/index.html", nil)
EXTJSROOT string
)
func init() {
var getenvErr os.Error
EXTJSROOT, getenvErr = os.Getenverror("EXTJSROOT")
if getenvErr != nil {
panic(getenvErr)
}
}
// view example at http://localhost:8080/ with firebug console
// ext docs at http://localhost:8080/ext/docs/
// for go docs run "godoc -http=:6060" and hit http://localhost:6060/
func main() {
示例7: root
}
func root(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
q := datastore.NewQuery("Greeting").Order("-Date").Limit(10)
greetings := make([]Greeting, 0, 10)
if _, err := q.GetAll(c, &greetings); err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
return
}
if err := guestbookTemplate.Execute(w, greetings); err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
}
}
var guestbookTemplate = template.MustParseFile("views/guestbook.html", nil)
func sign(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
g := Greeting{
Content: r.FormValue("content"),
Date: datastore.SecondsToTime(time.Seconds()),
}
if u := user.Current(c); u != nil {
g.Author = u.String()
}
_, err := datastore.Put(c, datastore.NewIncompleteKey("Greeting"), &g)
if err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
return
}
示例8: init
func init() {
for _, tmpl := range []string{"base"} {
templates[tmpl] = template.MustParseFile("templates/"+tmpl+".html", nil)
}
}
示例9: init
// data that gets processed by the html template
type HuntTemplateData struct {
User string
Error string
SuppressAnswerBox bool
SuppressBackButton bool
*HuntDirectoryEntry
DebugHuntData *Hunt
CurrentState *State
}
func init() {
http.HandleFunc(huntPath, handleHunt)
}
var huntTemplate = template.MustParseFile(huntTemplateFileName, template.FormatterMap{"dstime": dstimeFormatter})
func handleHunt(w http.ResponseWriter, r *http.Request) {
var td HuntTemplateData
var err os.Error
defer recoverUserError(w, r)
c = appengine.NewContext(r)
td.User = requireAnyUser(w, r)
LogAccess(r, td.User)
if td.User == "" {
panic("requireAnyUser did not return a username")
}
// get hunt name from URL path
huntSearchName := strings.Split(strings.Replace(r.URL.Path, huntPath, "", 1), "/", 2)[0]
示例10: checkError
Threshold float64
RotationStride float64
MatchStride int
MatchingOffset int
GammaAdjust float64
AverageBias float64
}
type Work struct {
conn *websocket.Conn
input *ProcessInput
stopCh chan bool
}
var (
uploadTemplate = template.MustParseFile(TemplateDir+"upload.html", nil)
errorTemplate = template.MustParseFile(TemplateDir+"error.html", nil)
workChan = make(chan Work)
)
/*
* Check for error and panic if needed
*/
func checkError(e os.Error) {
if e != nil {
panic(e)
}
}
/*
* Index page handler
示例11: parseTemplate
func parseTemplate(filename string) *Template {
return &Template{
t: template.MustParseFile(path.Join("template", filename), formatterMap),
mimeType: mime.TypeByExtension(path.Ext(filename))}
}
示例12: serve404
// Used only for debugging.
import (
"reflect"
)
import (
"controller"
"model"
)
/* HTML templates.
*
* These are stored in the top level directory of the app.
*/
var (
signInTemplate = template.MustParseFile("sign.html", nil)
streamTemplate = template.MustParseFile("stream.html", nil)
dashTemplate = template.MustParseFile("dashboard.html", nil)
profileTemplate = template.MustParseFile("profile.html", nil)
)
/* Serve a Not Found page.
*
* TODO: Add a styled template.
*/
func serve404(w http.ResponseWriter) {
w.WriteHeader(http.StatusNotFound)
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
io.WriteString(w, "Not Found")
log.Println("ERROR: 404 Not Found")
}
示例13: config
)
// config returns the configuration information for OAuth and Buzz.
func config(host string) *oauth.Config {
return &oauth.Config{
ClientId: CLIENT_ID,
ClientSecret: CLIENT_SECRET,
Scope: "https://www.googleapis.com/auth/buzz",
AuthURL: "https://accounts.google.com/o/oauth2/auth",
TokenURL: "https://accounts.google.com/o/oauth2/token",
RedirectURL: fmt.Sprintf("http://%s/post", host),
}
}
var (
uploadTemplate = template.MustParseFile("upload.html", nil)
editTemplate *template.Template // set up in init()
postTemplate = template.MustParseFile("post.html", nil)
errorTemplate = template.MustParseFile("error.html", nil)
)
// Because App Engine owns main and starts the HTTP service,
// we do our setup during initialization.
func init() {
http.HandleFunc("/", errorHandler(upload))
http.HandleFunc("/edit", errorHandler(edit))
http.HandleFunc("/img", errorHandler(img))
http.HandleFunc("/share", errorHandler(share))
http.HandleFunc("/post", errorHandler(post))
editTemplate = template.New(nil)
editTemplate.SetDelims("{{{", "}}}")
示例14: init
func init() {
for _, tmpl := range []string{"edit", "view"} {
templates[tmpl] = template.MustParseFile(tmpl+".html", nil)
}
}
示例15: stepHandler
var step1 = Step{
"Double my input",
"Write a function that doubles its input.",
"func fn(i int) int {\n}",
TestData{Setup: "n := 1", Args: "n", Expect: "2"},
}
func stepHandler(w http.ResponseWriter, r *http.Request) {
err := stepTemplate.Execute(w, step1)
if err != nil {
log.Print(err)
}
}
var stepTemplate = template.MustParseFile("tmpl/step.html", nil)
func testHandler(w http.ResponseWriter, r *http.Request) {
// read user code
userCode := new(bytes.Buffer)
defer r.Body.Close()
if _, err := userCode.ReadFrom(r.Body); err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
return
}
// put user code in harness
t := step1.Test
t.UserCode = userCode.String()
code := new(bytes.Buffer)
if err := TestHarness.Execute(code, t); err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)