Tea Template Language

jappy edited this page Jan 20, 2012 · 21 revisions

Tea is a strongly typed, compiled programming language designed to work within a Java-based hosting environment.

  1. Why Use Tea
  2. Embedding Tea
  3. Lexical Structure
  4. Tea Templates
  5. Tea Functions
  6. Type Conversion
  7. Grammar Summary
  8. Tea Architecture
  9. Document Revisions



Tea's main goal is to provide an enforced separation between data processing and presentation, without sacrificing basic programming constructs. Tea modules are called templates, and they are used for data presentation. Templates are generally used for textual output and HTML, and thus text printing operations are designed to be as simple as possible. A template cannot directly acquire or modify information. It relies on its host to provide data. Data access operations are as important as printing output and are also very simple.

Tea's basic constraints:

  • Data or data types cannot be directly created. They are acquired.
  • Acquired data cannot be directly modified in any way.
  • A template cannot directly cause harm to its hosting environment.
  • Only the minimum amount of programming constructs is provided.

Because of the above constraints, Tea cannot do anything useful without a hosting environment. Tea’s host must be Java-based, and all data are treated as objects. Properties are accessed from objects by treating them as JavaBeans. The restricted programming model forces complex programming tasks to be performed in Java, and this is how presentation is kept separate from processing.

Here’s a very basic "Hello World" template that reads a message property from an object:

<% template HelloWorld(ContentObject obj) %>
Hello <% obj.message %>!

The "<%" and "%>" symbols delimit code regions, and text not in any code regions is printed as is. The above template may also be written in a single code region as:

<% template HelloWorld(ContentObject obj)
"Hello " obj.message

The way in which an instance of ContentObject is passed into the above template is left up to the host. Often, data is acquired through custom functions, which must be written in Java.

The set of programming constructs provided in Tea are designed to resemble Java in syntax, except semi-colons are not required to terminate statements. Like Java, the Tea character set is Unicode. Tea has an if statement, just like Java’s. Tea does not, however, have a switch statement, as it is not necessary. Looping in Tea is provided via a foreach statement. There is not any while or for statement.

With the exception of passed-in parameters, local variables may be dynamically or declaratively typed. Variable declarations are implicit. Variables can use primitive types or object references, but all data appears as if they were objects. Yet Tea is still strongly and statically typed.

Tea templates are compiled directly to Java byte code, without the use of a Java compiler. Tea does not use any special kind of interpreter. Tea templates perform as well as hand-written Java code. Any runtime exception caused while executing a template contains a stack trace that identifies which template was executing and what line number was executing.

Because Tea must be integrated into a hosting environment, the implementation is very modular at both the compiler and runtime levels. Tea templates can be used to generate dynamic or static HTML pages. There is no restriction on how data is output. Most Tea environments that support running of Tea templates also support safe, dynamic template re-compilation. Any compilation errors are reported in a fashion that best suits that environment. A web-based compilation page, for example, will show detailed compilation errors on the web page.

Next: Why Use Tea >>