Gin是一款輕量級的Web框架,被廣泛應用於Go語言的Web開發中。在Gin中,Web應用程式通常只需要渲染一個模板即可完成頁面的展示。這種設計使得開發者可以更專注於業務邏輯的實現,簡化了開發流程。在php小編小新的觀點中,Gin的這種特性不僅提高了開發效率,還能減少資源的佔用,使得Web應用程式更有效率。同時,Gin也提供了豐富的中間件和插件,為開發者提供了更多的擴充性和靈活性。總之,Gin的簡潔而強大的特性使得它成為了許多開發者的首選框架。
問題內容
我有一個 Gin Web 應用程序,其中包含基於一組部分和一個基本模板的多個 HTML 模板。基本模板似乎與相關部分一起渲染得很好,但我的主要視圖、登入、索引和註冊沒有按預期渲染。每當我存取其中任何一個的 HTTP 端點時,只會呈現暫存器視圖。
以下文件中缺少或配置錯誤的內容導致我的路由無法呈現請求的頁面?
我的專案有以下結構。
├── app ... │ ├── handlers │ │ ├── general │ │ │ └── general.go │ │ └── routes.go │ ├── main.go │ ├── reloadDev.sh │ ├── static │ │ ├── css │ │ ├── img │ │ └── js │ └── templates │ ├── home │ │ ├── index.tmpl.html │ │ ├── login.tmpl.html │ │ └── register.tmpl.html │ ├── layouts │ │ └── base.tmpl.html │ └── partials │ ├── footer.tmpl.html │ ├── head.tmpl.html │ └── navbar.tmpl.html
base.tmpl.html
{{ define "base" }} <!DOCTYPE html> <html lang="eng" data-bs-theme="dark"> {{ template "head" . }} {{template "navbar" .}} <body> {{ block "content" . }}{{ end }} </body> {{template "footer" .}} </html> {{end}}
註冊.tmpl.html
{{ template "base" . }} {{ define "content" }} <div class="container"> <div class="row"> <div class="col-md-6 offset-md-3"> <h1 id="Register">Register</h1> <form action="/register" method="post"> <div class="mb-3"> <label for="username" class="form-label">Username</label> <input type="text" name="username" id="username" class="form-control" placeholder="Username" required> </div> <div class="mb-3"> <label for="password" class="form-label">Password</label> <input type="password" name="password" id="password" class="form-control" placeholder="Password" required> </div> ...SNIP... <button type="submit" class="btn btn-primary">Register</button> </form> </div> </div> </div> {{ end }}
index.tmpl.html(登入的結構與這兩個相同。)
{{ template "base" . }} {{ define "title" }}Home{{ end }} {{ define "content" }} <div class="container"> <div class="row"> <div class="col-md-6 offset-md-3"> <p>Welcome to Astra Porta.</p> <p>Click <a href="https://www.php.cn/link/1b8e84dcae97ad25234484e38615c570">here</a> to login.</p> </div> </div> </div> {{ end }}
HTML 範本使用 embed.FS
與二進位檔案捆綁在一起。
//go:embed templates/partials/* templates/layouts/* templates/home/* var files embed.FS func main() { router := setupRouter() err := router.Run() if err != nil { panic(err) } } func setupRouter() *gin.Engine { router := gin.Default() subFS, e := fs.Sub(files, "templates") if e != nil { panic(e) } tmpl := template.Must(template.ParseFS( subFS, "layouts/*.html", "partials/*.html", "home/*.html", )) router.SetHTMLTemplate(tmpl) router.StaticFS("/static", http.Dir("static")) err := router.SetTrustedProxies(nil) if err != nil { panic(err) } handlers.InitializeRoutes(&router.RouterGroup) return router }
頁面在我的應用程式路由中呈現。此處的引用會對應到 *.tmpl.html
檔案的檔案名稱。
func SiteIndex(c *gin.Context) { c.HTML(http.StatusOK, "index.tmpl.html", nil) } func GetRegister(c *gin.Context) { c.HTML(http.StatusOK, "register.tmpl.html", nil) } func GetLogin(c *gin.Context) { c.HTML(http.StatusOK, "login.tmpl.html", nil) }
解決方法
對於其他遇到此問題的人。 mkopriva 評論中指出的解決方案是正確的。我刪除了 base.tmpl.html
並使用更新的部分和目標頁面組成每個視圖。
標題
{{ define "header" }} <!DOCTYPE html> <html lang="eng" data-bs-theme="dark"> {{template "navbar" .}} <body> {{ block "content" . }}{{ end }} <head><meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> ...SNIP... <title>App</title> </head> {{end}}
頁尾
{{define "footer"}} <div class="container"> <footer class="py-3 my-4" data-bs-theme="dark"> <ul class="nav justify-content-center border-bottom pb-3 mb-3"> <li class="nav-item"><a href="/" class="nav-link px-2 text-body-secondary">Home</a></li> </ul> <p class="text-center text-body-secondary">© 2024 .</p> </footer> </div> </body> </html> {{end}}
有問題的頁面
{{template "header"}} <div class="container"> <div class="row"> <div class="col-md-6 offset-md-3"> <p>Welcome to Astra Porta.</p> <p>Click <a href="https://www.php.cn/link/1b8e84dcae97ad25234484e38615c570">here</a> to login.</p> </div> </div> </div> {{template "footer"}}
以上是Gin Web 應用程式僅渲染一個模板的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Golangisidealforbuildingscalablesystemsduetoitsefficiencyandconcurrency,whilePythonexcelsinquickscriptinganddataanalysisduetoitssimplicityandvastecosystem.Golang'sdesignencouragesclean,readablecodeanditsgoroutinesenableefficientconcurrentoperations,t

Golang在並發性上優於C ,而C 在原始速度上優於Golang。 1)Golang通過goroutine和channel實現高效並發,適合處理大量並發任務。 2)C 通過編譯器優化和標準庫,提供接近硬件的高性能,適合需要極致優化的應用。

選擇Golang的原因包括:1)高並發性能,2)靜態類型系統,3)垃圾回收機制,4)豐富的標準庫和生態系統,這些特性使其成為開發高效、可靠軟件的理想選擇。

Golang適合快速開發和並發場景,C 適用於需要極致性能和低級控制的場景。 1)Golang通過垃圾回收和並發機制提升性能,適合高並發Web服務開發。 2)C 通過手動內存管理和編譯器優化達到極致性能,適用於嵌入式系統開發。

Golang在編譯時間和並發處理上表現更好,而C 在運行速度和內存管理上更具優勢。 1.Golang編譯速度快,適合快速開發。 2.C 運行速度快,適合性能關鍵應用。 3.Golang並發處理簡單高效,適用於並發編程。 4.C 手動內存管理提供更高性能,但增加開發複雜度。

Golang在Web服務和系統編程中的應用主要體現在其簡潔、高效和並發性上。 1)在Web服務中,Golang通過強大的HTTP庫和並發處理能力,支持創建高性能的Web應用和API。 2)在系統編程中,Golang利用接近硬件的特性和對C語言的兼容性,適用於操作系統開發和嵌入式系統。

Golang和C 在性能對比中各有優劣:1.Golang適合高並發和快速開發,但垃圾回收可能影響性能;2.C 提供更高性能和硬件控制,但開發複雜度高。選擇時需綜合考慮項目需求和團隊技能。

Golang适合高性能和并发编程场景,Python适合快速开发和数据处理。1.Golang强调简洁和高效,适用于后端服务和微服务。2.Python以简洁语法和丰富库著称,适用于数据科学和机器学习。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3漢化版
中文版,非常好用

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

SublimeText3 Linux新版
SublimeText3 Linux最新版

WebStorm Mac版
好用的JavaScript開發工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),