Templating in Go

Share on Facebook5Tweet about this on Twitter36Share on Google+5Share on Reddit0Share on StumbleUpon21

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.

500px-TempEngWeb016.svg

Why Templating?

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:

Screen Shot 2014-05-29 at 11.20.06 PM

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:

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:

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

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

Building a Web Server in Go: Handling Page Requests
Building a Web Server in Go: Handling User Input
Building a Web Server in Go: Authentication Cookies
Building a Web Server in Go: Database Accesses
Abstracting in Go

Share on Facebook5Tweet about this on Twitter36Share on Google+5Share on Reddit0Share on StumbleUpon21

 Subscribe

If you enjoyed this article subscribe via email, it's free!

Plus exclusive email only content and problem solutions.

View Archived Emails (no surprises, no spam!)

One comment on “Templating in Go

  1. Though I only followed about half of this, I’m glad I clicked. I learned something new about how websites are built. I learned some HTML a long time ago just because it was easy, but I never got beyond that for site-building.

Leave a Reply

Your email address will not be published.


nine − = 4

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">