Tools

  • Register

Wichtige Links

The Tricia Templating System - Basics

Tags: no tags assigned  

Tricia has its own templating language, which is a so-called restricted template language. Restricted means, that there is a strict enforced separation of presentation and logic, since it is not possible to encode logic in the template language. For a more detailed discussion on this topic see http://davidrupp.blogspot.com/2007/06/required-reading-enforcing-strict- model.html and the referenced paper at http://www.cs.usfca.edu/~parrt/papers/mvc.templates.pdf.

The main idea of a templating language is that a template file - which is in most cases an HTML file - contains the basic layout of a content element together with placeholders which are substituted dynamically for the actual contents at runtime. The Tricia template language uses the '$' character as the main delimiter for defining placeholders. If the output should contain the $ sign, it has to be escaped by $$. An example placeholder is $name$. In a template it could be used like this:

<p>Hello, my name is $name$</p>

Corresponding to each placeholder there is a substitution in the Java code, which dynamically controls what will be substituted at runtime. The link between a substitution and the template file is established via an instance of the Template class which holds a link to the respective template file and can be considered as a container for the substitutions. Substitutions are defined as anonymous inner classes. For the example above the substitution might look like this (assume an object named 'person' is visible in this context):

  Template template = new Template();
template.put("name", new PrintSubstitution() {
public String print() {
return person.getName();
}
});

Substitutions are defined in Toro using an introspective programming model. This makes it possible, that the consistency between the template which contains the placeholders and the Java code which contains the substitutions can be checked automatically. For example spelling mistakes in the placeholders and the substitution keys can be found this way. This is done by the Consistency Checker (see ConsistencyCheckDoc).

The Tricia template language has four basic kinds of substitutions:

  • print substitution,
  • conditional substitution,
  • list substitution, and
  • template substitution,

which will be covered in the following.

In addition there are so-called function substitutions which differ from the basic substitutions in so far that they are not bound to an instance of the Template object (pointing to a single file) but the respective placeholders can appear in any template file. Function subsitutions are explained on the page Function Subsitutions.

Print Substitution

This is the simplest substitution type, which replaces the placeholder by a dynamically computed string.

Grammar:

 PrintSubstitution:
$ Tag $

In the Java code print substitutions are specified using the class PrintSubstitution. There is an Eclipse template (see EclipseTemplatesDoc), which creates the skeleton of a print substitution: printSubstitution.

Example:

TemplateJava codeOutput
 Name: $name$
 template.put("name", new PrintSubstitution() {
public String print() {
return person.getName();
}
});
 Name: Thomas Buechner

Conditional Substitution

This substitution is used to control the output of blocks of template code similar to the execution of code blocks in an if-then-else statement. These blocks may contain placeholders.

Grammar:

 ConditionalSubstitution:
$[ Tag $
Content 1
$] Tag [$
Content 2
$ Tag ]$

or

                     $[ ! Tag $ 
Content 1
$] Tag [$
Content 2
$ Tag ]$

In the Java code conditional substitutions are specified using the class ConditionalSubstitution. There is an Eclipse template (see EclipseTemplatesDoc), which creates the skeleton of a conditional substitution: conditionalSubstitution.

Example:

TemplateJava codeOutput
 The condition is 
$[condition$
true.
$]condition[$
false.
$condition]$
 template.put("condition", new ConditionalSubstitution() {
public boolean test() {
return true;
}
});
 The condition is true.

List Substitution

This substitution is used to output a block of a template repeatedly. The block may contain additional placeholders itself. One requirement is, that the placeholders inside a list block can refer to the state of the current iteration. This is done by defining a qualifier in the placeholder definition, which is then used to refer to the current item. Precisely the placholder doesn't refer to the current item but to a substitution defined within the list substitution. There the current item can be accessed (see example below).

Grammar:

 ListSubstitution:
$[ Tag Qualifier $
Content
$|$
Content
$ Tag ]$

The second part is used for the last item in the list. In the Java code list substitutions are specified using the class ListSubstitution. There is an Eclipse template (see EclipseTemplatesDoc), which creates the skeleton of a listsubstitution: listSubstitution.

Example:

TemplateJava codeOutput
 Liste: $[list e$
$e.print$ , $|$
$e.print$
$list]$.
 template.put("list", new ListSubstitution() {
Iterator<Integer> i;

Integer current;

public void start() {
i = list.iterator();
}

public boolean hasNext() {
return i.hasNext();
}

public void next() {
current = i.next();
}

public void putSubstitutions(Template t) {
t.put("print", new PrintSubstitution() {
public String print() {
return current.toString();
}
});
}
});
 Liste: erstens, zweitens, drittens.

Template Substitution

This substitution type is used to replace a placeholder with the output of an evaluated template. Using template substitutions complex nested structures can be built.

The placeholder for a template substitution looks the same as for a print substitution, since the effect on the template is the same - the placeholder will at runtime be replaced with dynamically generated content. In the Java code template substitutions are defined using the class TemplateSubstitution.

An example with all basic substitution types

The BasicSubstitutionsHandler shows simple examples of all substitution types. It can be accessed by the URL /examples/basicSubstitutions.

 

0 Comments

Leave a comment: