# Cheetah Template One-Page Tutorial
By Chuck Esterbrook, now maintained by Mike Orr
Updated May 17, 2002

## Substitution

The most commonly used templating feature is substitution:

>     My name is $name

You can use dotted notation to traverse objects. For example:

>     $author.person.primaryEmail

When a variable name is flush against other text you must use curly braces to separate it:

>     Jim is a ${vice}maniac.

However, the template language is savvy enough to handle sentence endings and variables:

>     My name is $name.
>     $foo$bar

## #if (conditional)

The #if statement can be used to detect if a value is blank/undefined/NULL. The most common use for an #if statement is showing a different form of content if something is undefined.

>     #if $keywords
>         $keywords
>     #else
>         There are no keywords!
>     #end if

The #else part is optional:

>     #if $keywords
>         <span class=keywords>$keywords</span>
>     #end if

## #for (looping)

The #for statement is used to loop through any kind of list.

>     #for $author in $authors
>         $author.person.fullName
>     #end for

## Combining statements

You can combine statements. For example,

Template.html:

>     $name

>     <p> $content

>     #include "authors.html"

Authors.html:

>     #if $authors
>         <table class=Author>
>         #for $author in $authors
>             <tr class=Author> <td class=Author>
>                 $author
>             </td> </tr>
>         #end for
>         </table>
>     #end if

## Examples

Here is an example that lists several names and puts commas between them. Note the use of #slurp to slurp up extra whitespace that might interfere.

>     #set sep = ''
>     #for $author in $authors
>     ${sep}${author.person}#slurp
>     #set sep = ', '
>     #end for