Go, AppEngine: how to structure application templates

How do people handle using templates in their AppEngine apps on Go?

In particular, I am looking for a project structure that gives the following:

  • Hierarchical structure (catalog) of patterns and partial patterns
  • Let me use HTML tools / editors on my templates (embedding template text in xxx.go files makes execution difficult)
  • Automatically reload template text on dev server

Potential stumbling blocks:

  • template.ParseGlob () will not pass recursively.
  • For performance reasons, it is recommended that you do not load your templates as raw text files (because these text files are on different servers than when executing the code).

Please note that I am not looking for a tutorial / template package usage examples. This is a question about the structure of the application. Moreover, if you have a code that solves the above problems, I would love to see it. Thanks in advance.

+45
google-app-engine go
Mar 05 2018-12-12T00:
source share
2 answers

One of my favorite Go features is the ability to easily add handlers inside packages. This greatly simplifies the process of writing modular code.

Example:

File structure

|-- 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 

Each package has an http.go file that uses the url prefix. For example, the products package under github.com/storeski/appengine/products should have any incoming URL starting with /products .

With this modular approach, it is useful to store templates in the products package. If you want to keep the basic template for the site, you can set an agreement in which you extend templates/base.html .

Example

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) } } 

This approach is very interesting because it makes sharing applications / packages trivial. If I write a package that handles authentication, which takes responsibility for the /auth url. Any developer who then adds a package to their product root instantly has all the functionality. All they need to do is create a basic template ( templates/base.html ) and direct their users to /auth .

+68
Mar 06 '12 at 16:18
source share

Sorry in advance, because this message is not what you are really looking for, and you may have already heard what I'm going to say a million times. This is better than no posts at all, so here it is:

Go 1 will be out soon (in a week or two). I'm sure App Engine will soon switch to Go 1 support for r60. The corelibs templates (among other libs) got a decent amount during this time, so it's kind of a mess to find a popular way to do things related to oneself due to the many changes that happen through the language.

This suggests that I saw quite a few people who worked in these different ways, but very few of them were specific to AppEngine, because most of the work done in Go is constantly updated with the language (which is incompatible with r60 for a long time). If you want to see some kind of code that people used for such projects, you should jump on IRC and ask. Templates are a popular topic, and I only used the basic functions with them - I never touched sets. IRC is very friendly and you can learn a lot there. This is definitely the best resource besides documents right now for the language. If you do not already know that the IRC channel has # go-nut on FreeNode.

Thanks for reading and good luck developing in App Engine. I hope the Go 1 updates begin quickly.

0
Mar 06 2018-12-12T00:
source share



All Articles