From Wikipedia:

A web template system uses a template processor to combine web templates to form finished web pages, possibly using some data source to customize the pages or present a large amount of content on similar-looking pages. It is a web publishing tool present in content management systems, web application frameworks, and HTML editors. Web templates can be used like the template of a form letter to either generate a large number of “static” (unchanging) web pages in advance, or to produce “dynamic” web pages on demand.

Why T emplating ?

One prime example of templating occurs when handling HTML page requests for a web server you often need to load unique to a user, whether it is viewing history, purchases, saved articles, advertisements, etc. However, in order to display user specific data a web server must have a way to display either different pages with preloaded data or various partitions of a page which we can update at runtime. If you consider for a moment how silly it would be to create your Facebook or Twitter feed an hour before you need it, it only makes sense to update partitions of a web page upon request (at least for an interactive website).

What this means is that every time a user requests a webpage the various partitions of a webpage are arranged and sent to the user. For example, most webpages contain a header, an article, an RSS feed or blogroll, and maybe a few advertisements:

As you can see we can layout the various partitions in different ways, giving each partition a different size, but we could also have a different color, texture, etc. Essentially, each partition is treated almost as if it is a separate webpage with its own parameters, often defined by the CSS style sheet (though even this can be). Since each partition can be loaded separately there are several advantages:

(a) update a partition without having to rebuild the whole page.

(b) Save memory space by constructing a webpage upon request.

(c) Construct each partition concurrently via multithreading.

Go and Templating

A trivial example using, import “text/template” from golang.org:

View the code on Gist.

As you can see the double brackets {{ }} with the dot-object-name “{{.<Object Name> }} “stands in” or creates a form which can be filled by various input concurrently. In the example above the output would be “17 items are made of wool.”

A slightly more complicated example using, import “html/template”

Package template (html/template) implements data-driven templates for generating HTML output safe against code injection. It provides the same interface as package text/template and should be used instead of text/template whenever the output is HTML.

One of the benefits of using Go is that it is much faster than PHP, Python or Ruby when completing most tasks, often comparable to C++ [1]. With each partition being loaded/constructed separately at runtime this makes templating more appealing using Go than say PHP, Python or Ruby.

Given templated HTML code such as the following:

View the code on Gist.

Using code from a post on handling page requests, we can generate custom pages based on the user info:

View the code on Gist.

What the above templating allowed us to do is create a custom, secure webpage which is generated from various database calls, and loaded into particular positions on the page.

Related Articles