栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

开始,AppEngine:如何为应用程序构建模板

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

开始,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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/471111.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号