本文整理汇总了Golang中html/template.ParseGlob函数的典型用法代码示例。如果您正苦于以下问题:Golang ParseGlob函数的具体用法?Golang ParseGlob怎么用?Golang ParseGlob使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ParseGlob函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: watchAndParseTemplates
func watchAndParseTemplates() {
templatesDir := utils.FindDir("web/templates")
l4g.Debug(utils.T("web.parsing_templates.debug"), templatesDir)
var err error
if Templates, err = template.ParseGlob(templatesDir + "*.html"); err != nil {
l4g.Error(utils.T("web.parsing_templates.error"), err)
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
l4g.Error(utils.T("web.create_dir.error"), err)
}
go func() {
for {
select {
case event := <-watcher.Events:
if event.Op&fsnotify.Write == fsnotify.Write {
l4g.Info(utils.T("web.reparse_templates.info"), event.Name)
if Templates, err = template.ParseGlob(templatesDir + "*.html"); err != nil {
l4g.Error(utils.T("web.parsing_templates.error"), err)
}
}
case err := <-watcher.Errors:
l4g.Error(utils.T("web.dir_fail.error"), err)
}
}
}()
err = watcher.Add(templatesDir)
if err != nil {
l4g.Error(utils.T("web.watcher_fail.error"), err)
}
}
示例2: watchAndParseTemplates
func watchAndParseTemplates() {
templatesDir := utils.FindDir("web/templates")
l4g.Debug("Parsing templates at %v", templatesDir)
var err error
if Templates, err = template.ParseGlob(templatesDir + "*.html"); err != nil {
l4g.Error("Failed to parse templates %v", err)
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
l4g.Error("Failed to create directory watcher %v", err)
}
go func() {
for {
select {
case event := <-watcher.Events:
if event.Op&fsnotify.Write == fsnotify.Write {
l4g.Info("Re-parsing templates because of modified file %v", event.Name)
if Templates, err = template.ParseGlob(templatesDir + "*.html"); err != nil {
l4g.Error("Failed to parse templates %v", err)
}
}
case err := <-watcher.Errors:
l4g.Error("Failed in directory watcher %v", err)
}
}
}()
err = watcher.Add(templatesDir)
if err != nil {
l4g.Error("Failed to add directory to watcher %v", err)
}
}
示例3: LoadHTMLGlob
func (engine *Engine) LoadHTMLGlob(pattern string) {
if IsDebugging() {
debugPrintLoadTemplate(template.Must(template.ParseGlob(pattern)))
engine.HTMLRender = render.HTMLDebug{Glob: pattern}
} else {
templ := template.Must(template.ParseGlob(pattern))
engine.SetHTMLTemplate(templ)
}
}
示例4: startServer
func startServer(port string) {
manager = session.NewSessionManager(nil)
manager.OnStart(func(session *session.Session) {
l.Debug("Session Manager: Started a new session.\n")
})
manager.OnEnd(func(session *session.Session) {
l.Debug("Session Manager: Destroyed a session.\n")
})
manager.SetTimeout(10)
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("web/static/"))))
http.HandleFunc("/data", DataHandler)
http.HandleFunc("/", IndexHandler)
http.HandleFunc("/login", LoginHandler)
var e error
t, e = template.ParseGlob("web/templates/*.tmpl")
if e != nil {
l.Fatal("Unable to parse templates: %s\n", e.Error())
}
e = http.ListenAndServe(port, nil)
if e != nil {
l.Fatal("Unable to start embeeded webserver: %s\n", e.Error())
}
}
示例5: init
func init() {
tpl, _ = template.ParseGlob("*.html")
http.HandleFunc("/", main)
//serve the css files in a file server instead of uploading it to gcs and querying it.
http.Handle("/css/", http.StripPrefix("/css", http.FileServer(http.Dir("css"))))
}
示例6: main
func main() {
tpl = template.Must(template.ParseGlob("templates/*.gohtml"))
f, err := os.OpenFile("logfile.txt", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0664)
if err != nil {
panic(err)
}
defer f.Close()
log.SetOutput(f)
client, err = as.NewClient("127.0.0.1", 3000)
if err != nil {
panic(err)
}
defer client.Close()
router := httprouter.New()
router.GET("/", index)
router.GET("/login", loginPage)
router.POST("/login", login)
router.GET("/logout", logout)
router.GET("/create", createPage)
router.POST("/create", create)
err = http.ListenAndServe(":9000", router)
if err != nil {
panic(err)
}
}
示例7: ParseGlob
func ParseGlob(pattern string) (Template, error) {
if Debug {
return (&reloadTemplate{}).ParseGlob(pattern)
}
t, err := htmlTmpl.ParseGlob(pattern)
return &instantTemplate{t}, err
}
示例8: adminUsersHandler
func adminUsersHandler(w http.ResponseWriter, r *http.Request, admin Location) {
isAuth(w, r, admin, "admin")
if r.Method == "POST" {
var user httpauth.UserData
user.Username = r.PostFormValue("username")
user.Email = r.PostFormValue("email")
password := r.PostFormValue("password")
user.Role = r.PostFormValue("role")
if err := aaa.Register(w, r, user, password); err != nil {
// maybe something
}
}
if user, err := aaa.CurrentUser(w, r); err == nil {
type data struct {
User httpauth.UserData
Roles map[string]httpauth.Role
Users []httpauth.UserData
Msg []string
}
messages := aaa.Messages(w, r)
users, err := backend.Users()
if err != nil {
panic(err)
}
d := data{User: user, Roles: roles, Users: users, Msg: messages}
var templates = template.Must(template.ParseGlob("admin/templates/*"))
t_err := templates.ExecuteTemplate(w, "manage-accounts.html", d)
if t_err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}
示例9: WrapHandlerImpl
// Wraps other http handlers. Creates context object, recovers from panics, etc.
func WrapHandlerImpl(fn AppHandlerFunc, parseForm bool) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
c := &Context{}
// See http://blog.golang.org/2010/08/defer-panic-and-recover.html.
defer func() {
if data := recover(); data != nil {
c.Aec().Errorf(fmt.Sprint(data))
ServeError(w, data)
}
}()
// Initialize the request context object.
c.SetAec(appengine.NewContext(r))
CheckError(ReadSession(r, c))
if msg, err := ConsumeFlash(w, r); err != nil && err != http.ErrNoCookie {
ServeError(w, err)
return
} else {
c.SetFlash(msg)
}
if parseForm {
CheckError(r.ParseForm())
}
if appengine.IsDevAppServer() {
tmpl = template.Must(template.ParseGlob("templates/*.html"))
text_tmpl = text_template.Must(text_template.ParseGlob("templates/*.txt"))
}
fn(w, r, c)
}
}
示例10: init
// set everything up.
func init() {
tpl = template.Must(template.ParseGlob("*.html"))
resourceHandler("css")
resourceHandler("img")
resourceHandler("photos")
http.HandleFunc("/", mainPage)
}
示例11: actionHome
func actionHome(w http.ResponseWriter, req *http.Request) {
var templActionHome *template.Template = template.Must(
template.ParseGlob("templates/home/*.html"))
lay := getLayoutTemplates()
wr := &HtmlContainer{}
// templActionHome.Funcs(template.FuncMap{"len": Len})
data := HtmlAssigner{
"List": getEntryList("", 10),
"Test": "Test",
}
err := templActionHome.Execute(wr, data)
if err != nil {
fmt.Errorf("%v", err)
}
lay.New("title").Parse("Najnowsze wpisy - " + config.GetStringDef("page", "title", ""))
err = lay.Execute(w, wr.getHtml())
if err != nil {
fmt.Errorf("%v", err)
}
}
示例12: loadTemplates
func loadTemplates() (templates *template.Template, err error) {
if !*devMode && t != nil {
return t, nil
}
t, err = template.ParseGlob("templates/*")
return t, err
}
示例13: listenForChanges
//reload templates on modify
func listenForChanges() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
done := make(chan bool)
// Process events
go func() {
for {
select {
case ev := <-watcher.Event:
log.Println("event:", ev)
//recompile templates
templates = template.Must(template.ParseGlob("templates/*.html"))
case err := <-watcher.Error:
log.Println("error:", err)
}
}
}()
err = watcher.Watch("templates/")
if err != nil {
log.Fatal(err)
}
<-done
watcher.Close()
}
示例14: helperMustLoadTemplates
func helperMustLoadTemplates() *template.Template {
t, err := template.ParseGlob(config.resourceDir + "/views/posts/*.html")
if err != nil {
panic(err.Error())
}
return t
}
示例15: initTemplate
func initTemplate() *template.Template {
t, parseErr := template.ParseGlob("template/*.tmpl")
if parseErr != nil {
log.Panicln("NisePostGo: ", parseErr)
}
return t
}