![]() |
||
|
||
![]() |
The simplest template is a plain HTML file. When the client visits that page, FreeMarker will send that HTML to the client as is. However if you want that page to be more dynamic then you begin to put special parts into the HTML which will be understood by FreeMarker:
${...} thing: FreeMarker will replace it in the output with the actual value of the thing in the curly brackets. They are called interpolations. As an example see the very first example.
FTL tags (for FreeMarker Template Language tags): FTL tags are similar to HTML tags, but they are instructions to FreeMarker and will not be printed to the output. The name of these tags start with #, so as to distinguish them clearly from HTML tags. (Just so I don't state false things: the name of certain FTL tags start with @ instead of #... this will be explained in later chapters.)
Comments: Comments are similar to HTML comments, but they are delimited by <#-- and --> (not <!-- and -->). Anything between these delimiters and the delimiter itself will be ignored by FreeMarker, and will not be written to the output.
Anything not an FTL tag or an interpolation or comment is considered plain text, and will not be interpreted by FreeMarker; it is just printed directly to the output.
With FTL tags you refer to so-called directives. This is the same as with HTML tags (e.g.: <table> and </table>) you refer to HTML elements (e.g. to the table element). If you don't feel the difference then just consider that ``FTL tag'' and ``directive'' are synonyms.
Though FreeMarker has far more directives, in this quick overview we will only look at three of the most commonly used ones.
With the if directive you can conditionally skip a section of the template. For example, assume that in the very first example you want to greet your boss differently from other users; the name of your boss is Big Joe:
![]() | ![]() | ![]() | |
![]() |
| ![]() | |
![]() | ![]() | ![]() |
Here you have told FreeMarker that the ``, our beloved leader'' should be there only if the value of the variable user is equal to the string "Big Joe". In general, things between <#if condition> and </#if> tags are skipped if condition is false.
Note that I have used two syntactical elements that were explained earlier. On the left side of = I have referenced a variable, and on the right side I have specified a literal string. You can write things like this too (using the data model used to demonstrate hashes):
![]() | ![]() | ![]() | |
![]() |
| ![]() | |
![]() | ![]() | ![]() |
With the <#else> tag you can specify what to do if the condition is false. For example:
![]() | ![]() | ![]() | |
![]() |
| ![]() | |
![]() | ![]() | ![]() |
This prints ``Pythons are cheaper than elephants today.'' if the price of python is less than the price of elephant, or else it prints ``Pythons are not cheaper than elephants today.''
This is useful when you want to list something. For example if you merge this template with the data model I used earlier to demonstrate sequences:
![]() | ![]() | ![]() | |
![]() |
| ![]() | |
![]() | ![]() | ![]() |
then the output will be:
![]() | ![]() | ![]() | |
![]() |
| ![]() | |
![]() | ![]() | ![]() |
The generic format of the list directive is:
<#list SequenceVar as variable>repeatThis</#list>
The repeatThis part will be repeated for each item in the sequence you have given as SequenceVar, one after the other, starting from the first item. In all repetitions variable will hold the value of the current item. This variable exists only within the <#list ...> and </#list>, and it does not overwrite any variables of the same name if they occur in the data model.
As another example, we list the fruits of that example data model:
![]() | ![]() | ![]() | |
![]() |
| ![]() | |
![]() | ![]() | ![]() |
Note that whatnot.fruits should be familiar to you; it references a variable in the data model.
With the include directive you can insert the content of another file into the template.
For example, suppose you have to show the same copyright notice on several pages. You can create a file that contains the copyright notice only, and insert that file everywhere where you need that copyright notice. Say, you store this copyright notice in copyright_footer.html:
![]() | ![]() | ![]() | |
![]() |
| ![]() | |
![]() | ![]() | ![]() |
And whenever you need that file you simply insert it with the include directive:
![]() | ![]() | ![]() | |
![]() |
| ![]() | |
![]() | ![]() | ![]() |
and the output will be:
![]() | ![]() | ![]() | |
![]() |
| ![]() | |
![]() | ![]() | ![]() |
If you change the copyright_footer.html, then the visitor will see the new copyright notice on all pages.
You can use directives as many times on a page as you want, and you can freely nest directives into each other. For example this will list the animals and print the name of large animals with bold:
![]() | ![]() | ![]() | |
![]() |
| ![]() | |
![]() | ![]() | ![]() |
Note that since FreeMarker does not interpret text outside FTL tags, interpolations and comments, it doesn't see the above <b> and </b> as wrongly nested tags.
![]() |
||
|
||
![]() |
![]() |
|
Page generated: 2004-06-15 22:17:59 GMT | FreeMarker Manual -- For FreeMarker 2.3 |