本文整理匯總了Golang中template.ParseFile函數的典型用法代碼示例。如果您正苦於以下問題:Golang ParseFile函數的具體用法?Golang ParseFile怎麽用?Golang ParseFile使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了ParseFile函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: init
func init() {
// Articles
goweb.MapRest("/article/category/{category_id}", new(controller.ArticleController))
// Articles
goweb.MapRest("/article", new(controller.ArticleController))
// Comments
goweb.MapRest("/comment", new(controller.CommentController))
// Categories
goweb.MapRest("/category", new(controller.CategoryController))
// Logs
goweb.MapRest("/log", new(controller.LogController))
// Search
goweb.MapRest("/search", new(controller.SearchController))
// Documentation wadl
goweb.MapFunc("/documentation/wadl", func(c *goweb.Context) {
var docTemplate = template.Must(template.ParseFile("webservice/views/wadl.wadl"))
if err := docTemplate.Execute(c.ResponseWriter, nil); err != nil {
c.RespondWithErrorMessage(err.String(), http.StatusInternalServerError)
}
})
// WADL in XML voor XML viewer
goweb.MapFunc("/documentation/wadl/xml", func(c *goweb.Context) {
var docTemplate = template.Must(template.ParseFile("webservice/views/wadl.xml"))
if err := docTemplate.Execute(c.ResponseWriter, nil); err != nil {
c.RespondWithErrorMessage(err.String(), http.StatusInternalServerError)
}
})
// Documentation
goweb.MapFunc("/documentation", func(c *goweb.Context) {
var docTemplate = template.Must(template.ParseFile("webservice/views/documentation.html"))
if err := docTemplate.Execute(c.ResponseWriter, nil); err != nil {
c.RespondWithErrorMessage(err.String(), http.StatusInternalServerError)
}
})
// Index
goweb.MapFunc("/", func(c *goweb.Context) {
var indexTemplate = template.Must(template.ParseFile("webservice/views/index.html"))
if err := indexTemplate.Execute(c.ResponseWriter, nil); err != nil {
c.RespondWithErrorMessage(err.String(), http.StatusInternalServerError)
}
})
// use the default Go handler
http.Handle("/", goweb.DefaultHttpHandler)
}
示例2: getTemplateFunc
// Get a template by filename and refresh if in development mode
func getTemplateFunc(filename string) func() *template.Template {
t, err := template.ParseFile(filename)
if err != nil {
log.Fatalf("Error parsing template: %v", err)
}
return func() *template.Template {
if Development {
t, err = template.ParseFile(filename)
if err != nil {
log.Printf("Error parsing template: %v", err)
}
}
return t
}
}
示例3: main
func main() {
prepareCache()
queryString := parseQueryString(os.Getenv("QUERY_STRING"))
fmap := template.FormatterMap{"html": template.HTMLFormatter}
opts := map[string](interface{}){}
os.Stdout.WriteString("Content-type: text/html\n\n")
conn := db.PrepareConnection(Cache["db"])
hdr, _ := template.ParseFile(Cache["tpl:headr"], fmap)
tmpl := new(template.Template)
if queryString["forum"] != "" {
tmpl, _ = template.ParseFile(Cache["tpl:forum"], fmap)
opts["forum"] = db.Get(conn, db.Forum{}, queryString["forum"])
} else {
tmpl, _ = template.ParseFile(Cache["tpl:index"], fmap)
}
_ = strconv.Itoa
c, _ := conf.ReadConfigFile(path.Join(Cache["wd"], "flow.config"))
opts["version"] = Version
opts["title"], _ = c.GetString("default", "boardName")
defer conn.Close()
if !db.TablesExist(conn) {
db.SetupForum(conn)
}
stmt, _ := conn.Prepare("SELECT id, name, desc FROM forum;")
db.HandleError(stmt.Exec())
forums := []db.Forum{}
if stmt.Next() {
var f db.Forum
db.HandleError(stmt.Scan(&f.Id, &f.Name, &f.Desc))
forums = append(forums, f)
}
opts["forums"] = forums
opts["cwd"] = Cache["cwdir"]
hdr.Execute(opts, os.Stdout)
tmpl.Execute(opts, os.Stdout)
}
示例4: EnterRollOff
func EnterRollOff(w http.ResponseWriter, r *http.Request) {
id := r.URL.Path[len("/roll-off-entry/"):]
fmt.Println("User wishes to enter rolloff ", id)
rollingUser := ParseUser(r)
entry := &RollOffEntry{User: rollingUser, Score: rand.Intn(100) + 1}
for _, r := range rolloffs {
fmt.Println("Checking rolloff ", r.Id)
if r.Id == id {
r.AddEntry(entry)
for elem := room.Users.Front(); elem != nil; elem = elem.Next() {
go func(e *list.Element) {
var tName string
u := e.Value.(*User)
if u == rollingUser {
tName = "templates/roll-off/user-joins.html"
} else {
tName = "templates/roll-off/other-user-joins.html"
}
t := template.Must(template.ParseFile(tName))
m := NewMessage("system", "", "system")
t.Execute(m, entry)
u.c <- m
}(elem)
}
}
}
}
示例5: loadTemplates
func loadTemplates() (err os.Error) {
PatientsTable, err = template.ParseFile("assets/patients.tpl", nil)
if err != nil {
log.Println("ERROR: Loading patients.tpl", err)
return err
}
log.Println("LOG: patients.tpl Loaded")
PatientsPage, err = template.ParseFile("assets/page.tpl", nil)
if err != nil {
log.Println("ERROR: Loading page.tpl", err)
return err
}
log.Println("LOG: page.tpl Loaded")
return nil
}
示例6: NewRollOff
func NewRollOff(w http.ResponseWriter, r *http.Request) {
rollingUser := ParseUser(r)
entry := &RollOffEntry{User: rollingUser, Score: rand.Intn(100) + 1}
rolloff := &RollOff{Id: randomString(20), Open: true}
rolloff.AddEntry(entry)
go rolloff.Cycle()
rolloffs = append(rolloffs, rolloff)
for elem := room.Users.Front(); elem != nil; elem = elem.Next() {
go func(e *list.Element) {
var tName string
u := e.Value.(*User)
if u == rollingUser {
tName = "templates/roll-off/rolling-user.html"
} else {
tName = "templates/roll-off/other-users.html"
}
t := template.Must(template.ParseFile(tName))
m := NewMessage("system", "", "system")
t.Execute(m, entry)
u.c <- m
}(elem)
}
}
示例7: HandleHome
func HandleHome(writer http.ResponseWriter, request *http.Request) {
if request.URL.Path == "/" {
temp, err := template.ParseFile("html/index.html")
if err != nil {
write_error(writer, err)
return
}
temp.Execute(writer, &Page{})
} else {
var err os.Error
var is_raw = false
var raw_prefix = "/raw/"
var urlid string
if strings.Contains(request.URL.Path, raw_prefix) {
is_raw = true
urlid = request.URL.Path[len(raw_prefix):]
} else {
urlid = request.URL.Path[1:]
}
page, err := GetPageFromDataStore(urlid, request)
if err != nil {
write_error(writer, err)
return
}
if is_raw {
RenderPageRaw(page, writer)
} else {
err = RenderPage(page, writer)
if err != nil {
write_error(writer, err)
return
}
}
}
}
示例8: loadTemplate
func loadTemplate(tname string) *templateInfo {
cached := templateCache[tname]
if cached == nil {
cached = &templateInfo{tpl: nil, mtime: 0, err: "Not loaded"}
}
// Check mtime.
fin, err := os.Open(tname)
if err != nil {
cached.err = fmt.Sprintf("Unable to open '%s': '%v'", tname, err)
return cached
}
defer fin.Close()
stat, err := fin.Stat()
if err != nil {
cached.err = fmt.Sprintf("Unable to stat '%s': '%v'", tname, err)
return cached
}
if stat.Mtime_ns <= cached.mtime {
return cached
}
cached.tpl, err = template.ParseFile(tname)
if err == nil {
cached.err = ""
} else {
cached.err = fmt.Sprintf("Error while parsing '%s': '%v'", tname, err)
}
templateCache[tname] = cached
return cached
}
示例9: viewHandle
func viewHandle(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFile("templates/view.html")
if err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
}
t.Execute(w, p)
}
示例10: GeneratePresentation
func GeneratePresentation(name, theme string) {
if dir, err := os.Getwd(); err == nil {
out := path.Join(dir, "out", name)
tDir := path.Join(dir, "themes", theme)
CopyDir(out, path.Join(dir, "resources"))
CopyDir(path.Join(out, "theme"), tDir)
templates := map[string] string{
"cfg.tpl": fmt.Sprintf("%s.cfg", name),
"fabfile.py.tpl": "fabfile.py",
"rst.tpl": fmt.Sprintf("%s.rst", name),
}
data := &TemplateData{name}
templatesDir := path.Join(dir, "templates")
for k, v := range templates {
t, err := template.ParseFile(path.Join(templatesDir, k))
if err != nil {
panic(err)
}
dstFile, _ := os.Create(path.Join(out, v))
defer dstFile.Close()
t.Execute(dstFile, data)
}
}
}
示例11: ParseFile
// parse a file, and store the templates in Page.Template
func (t *Page) ParseFile(formatterMap map[string]func(io.Writer, string, ...interface{})) {
var err os.Error
t.Template, err = template.ParseFile(t.Filename, formatterMap)
if err != nil {
log.Fatal("Cannot Parse " + t.Filename + "; got " + err.String())
}
}
示例12: RenderPage
func RenderPage(page *Page, writer http.ResponseWriter) os.Error {
temp, err := template.ParseFile("html/paste.html")
if err != nil {
return err
}
temp.Execute(writer, page)
return nil
}
示例13: root
func root(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
clientTemplate := template.Must(template.ParseFile("client.html"))
token_value := strconv.Itoa(rand.Int())
token_key, _ := channel.Create(c, token_value)
clientTemplate.Execute(w, token_key)
map_clients[token_key] = token_value
}
示例14: writeTemplate
func writeTemplate(w http.ResponseWriter, tempFile string, model interface{}) {
if tpl, err := template.ParseFile(tempFile); err != nil {
fmt.Fprintf(w, "Internal Server Error")
w.WriteHeader(500)
} else {
tpl.Execute(w, model)
}
}
示例15: logViewHandle
func logViewHandle(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFile("templates/logview.html")
if err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
}
pkg := r.URL.Path[9:]
t.Execute(w, p.Entries[pkg])
}