Templating Basics

Will Holcomb

18 January 2010

We'll start with the natural building block of any digital system: the bit. A bit is just a binary digit, a one or a zero. Bits are used to encode information. For example, imagine I have a bunch of red cards and a bunch of green cards. I give a red card to everyone in the country except for the President to whom I give a green one. Since the cards only have two possible states, they are a form of bit. The information associated with that bit, however, is highly complex.

Bits are combined into series to represent larger sets of possibilities. Whereas the President bit could only represent two possibilities: someone is the President (red card) or not the President (green card), a string of n bits can represent 2n possibilities. Almost all digital information in the world today is stored as groups of eight bits called "bytes."

Once again, context is important. Computers programs store information that tells the programs how the bits should be interpreted. For example, one of the more common needs is encoding a list of characters. In this situation, organizations have created specifications that describes which bytes should be mapped to which characters. So when the computer sees the byte 01100001, it knows that it represents the letter 'a'.

When multiple bytes are combined in a series, they become a "string." All digital information from webpages to e-mails to videos are strings. There are simple strings, like "sandwich" or "free fall", and more complex strings that are broken down incrementally according to a "file format." Files are simply pointers which direct the computer to the location of a string in storage.

The format of the file provides additional information about the context in which the string should be evaluated. One of the most common formats, is also one of the easiest to see the contextualization in action. It is called XML and it defines the basic rules that govern a variety of markup languages.

Markup languages take a piece of text and add additional metainformation to make the document richer. For example, webpages are written in a hypertext markup. If an author is writing about Dune and wants to represent that the string "Dune" is the title of a book, they surround it with "tags": <cite>Dune</cite>. When the browser reads the string, "<cite>Dune</cite>", it knows that the text within the tags ("Dune") is a citation and should be italicized.

One of the important characteristics of XML is it can be represented as a "tree." A tree is a structure for arranging strings relative to each other in a hierarchy. It is most easily understood in terms of an example. Consider this simple HTML string:

<html>
  <head><title>Greeting</title></head>
  <body>
    <p>Hello digital world!</p>
    <p>This is hypertext.</p>
  </body>
</html>

The tree for this document would look like:

A technology frequently used in creating webpages are "templating languages." These are a method of describing the structure of a page, but allowing the specific information to be tailored to the individual request. For example, a simple template in php might look like:

<html>
  <head><title>Greeting</title></head>
  <body><p>Hello <?php print $name ?></p></body>
</html>

The value of <?php print $name ?> is replaced when the page is loaded. So, for a template string, there is both the source string that defines the template and a result string that is produced by evaluating the template in a context. A tree representation of this source template would be: