php小編魚仔為您介紹一種強大的技巧:有條件地執行多個範本。在開發網站時,我們常常需要根據不同的條件動態地載入不同的模板文件,這就是有條件地執行多個模板的應用程式場景。透過使用這種技術,我們可以根據使用者的登入狀態、權限等條件來動態地載入相應的模板文件,從而實現更靈活、個人化的網站介面。這種技術不僅提高了網站的可擴展性和可維護性,還能為使用者提供更好的使用體驗。在本文中,我們將詳細介紹如何使用php實作有條件地執行多個範本的方法,幫助您更好地應用於實際專案中。
我有一個具有兩種視圖的網頁,一種用於匿名用戶,一種用於管理員用戶。我想僅為管理員用戶顯示導覽列。對於兩種使用者類型,其他一切都保持不變。
這是我迄今為止嘗試過的
package main import ( "log" "net/http" "text/template" "github.com/julienschmidt/httprouter" ) func basicauth(h httprouter.handle, requireduser, requiredpassword string) httprouter.handle { return func(w http.responsewriter, r *http.request, ps httprouter.params) { // get the basic authentication credentials user, password, hasauth := r.basicauth() if hasauth && user == requireduser && password == requiredpassword { // delegate request to the given handle h(w, r, ps) } else { // request basic authentication otherwise w.header().set("www-authenticate", "basic realm=restricted") http.error(w, http.statustext(http.statusunauthorized), http.statusunauthorized) } } } func anonymous(w http.responsewriter, r *http.request, _ httprouter.params) { t, err := template.parsefiles("index.html") if err != nil { log.fatalln(err) } err = t.execute(w, map[string]string{"name": "anonymous"}) if err != nil { log.fatalln(err) } } func admin(w http.responsewriter, r *http.request, _ httprouter.params) { t, err := template.parsefiles("index.html", "admin.html") if err != nil { log.fatalln(err) } err = t.execute(w, map[string]string{"name": "admin"}) if err != nil { log.fatalln(err) } } func main() { user := "admin" pass := "1234" router := httprouter.new() router.get("/", anonymous) router.get("/admin/", basicauth(admin, user, pass)) log.fatal(http.listenandserve(":8080", router)) }
<!doctype html> <html lang="en"> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>{{ .name }}</title> <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script> <script> function counter() { document.getelementbyid("x").innerhtml = "x: " + document.queryselectorall('.x').length; document.getelementbyid("y").innerhtml = "y: " + document.queryselectorall('.y').length; document.getelementbyid("z").innerhtml = "z: " + document.queryselectorall('.z').length; } </script> </head> <body onload="counter()"> {{ template "dashboard" }} <nav class="navbar fixed-bottom"> <div class="container-fluid nav-justified"> <span id="x" class="navbar-brand nav-item"></span> <span id="y" class="navbar-brand nav-item"></span> <span id="z" class="navbar-brand nav-item"></span> </div> </nav> </body> </html>
{{ define "dashboard" }} <nav class="navbar"> <div class="container-fluid nav-justified"> <span class="nav-item"> <a class="navbar-brand" href="/a">a</a> </span> <span class="nav-item"> <a class="navbar-brand" href="/b">b</a> </span> <span class="nav-item"> <a class="navbar-brand" href="/c">c</a> </span> </div> </nav> {{ end }}
我的假設是,因為我在為匿名使用者執行模板時沒有傳入 admin.html 模板,所以儀表板模板不會被解析。但是,我遇到了這個錯誤:
template: index.html:18:14: executing "index.html" at <{{template "dashboard"}}>: template "dashboard" not defined
如何解決這個問題,或是有更好的方法嗎?
使用 if
操作有條件地渲染 dashboard
範本:
{{ if eq .name "admin" }} {{ template "dashboard" }} {{ end }}
實踐是只解析模板一次,而不是在每個請求時解析它:
package main import ( "log" "net/http" "sync" "text/template" "github.com/julienschmidt/httprouter" ) func BasicAuth(h httprouter.Handle, requiredUser, requiredPassword string) httprouter.Handle { return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { user, password, hasAuth := r.BasicAuth() if hasAuth && user == requiredUser && password == requiredPassword { h(w, r, ps) } else { w.Header().Set("WWW-Authenticate", "Basic realm=Restricted") http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) } } } func Anonymous(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { err := tmpl.Execute(w, map[string]string{"Name": "Anonymous"}) if err != nil { log.Fatalln(err) } } func Admin(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { err := tmpl.Execute(w, map[string]string{"Name": "Admin"}) if err != nil { log.Fatalln(err) } } var ( tmpl *template.Template tmplOnce sync.Once ) func main() { user := "admin" pass := "1234" tmplOnce.Do(func() { tmpl = template.Must(template.ParseFiles("index.html", "admin.html")) }) router := httprouter.New() router.GET("/", Anonymous) router.GET("/admin/", BasicAuth(Admin, user, pass)) log.Fatal(http.ListenAndServe(":8080", router)) }
以上是有條件地執行多個模板的詳細內容。更多資訊請關注PHP中文網其他相關文章!