大佬教程收集整理的这篇文章主要介绍了开始,AppEngine:如何为应用程序构建模板,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我最喜欢的Go功能之一就是能够轻松地在包内添加处理程序。这大大简化了编写模块化代码的过程。
例如:
|-- app.yaml
|-- app
| +-- http.go
|-- templates
| +-- base.HTML
+-- github.com
+-- storeski
+-- appENGIne
|-- products
| |-- http.go
| +-- templates
| |-- List.HTML
| +-- detail.HTML
+-- account
|-- http.go
+-- templates
|-- overvIEw.HTML
+-- notifications.HTML
每个软件包都有一个http.go文件,该文件拥有url前缀的所有权。例如,products
下面的包github.com/storeski/appENGIne/products
将拥有任何以开头的入站URL/products
。
使用这种模块化方法,将模板存储在products
包中是有益的。如果您希望为站点维护一致的基本模板,则可以在扩展位置建立约定templates/base.HTML
。
templates / base.HTML
<!DOCTYPE HTML>
<HTML>
<head>
<title>{{.Store.titlE}}</title>
</head>
<body>
<div ID="content">
{{template "content" .}}
</div>
</body>
</HTML>
_github.com/storeski/appENGIne/products/templates/List.HTML_
{{define "content"}}
<h1> Products List </h1>
{{enD}}
_github.com/storeski/appENGIne/products/http.go_
func init() {
http.HandleFunc("/products", ListHandler)
}
var ListTmpl = template.Must(template.Parsefiles("templates/base.HTML",
"github.com/storeski/appENGIne/products/templates/List.HTML"))
func ListHandler(w http.ResponseWriter, r *http.request) {
tc := make(map[String]interface{})
tc["Store"] = Store
tc["Products"] = Products
if err := ListTmpl.Execute(w, tc); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
这种方法非常令人兴奋,因为它使应用程序/软件包的共享变得微不足道。如果我编写了一个处理身份验证的程序包,则该程序包获取/auth
URL的所有权。然后,任何将软件包立即添加到其产品根目录的开发人员都具有所有功能。他们要做的就是创建一个基本模板(templates/base.HTML
)并将其用户定向到/auth
。
以上是大佬教程为你收集整理的开始,AppEngine:如何为应用程序构建模板全部内容,希望文章能够帮你解决开始,AppEngine:如何为应用程序构建模板所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。