examples are for v3 not v4
This short introduction to StringTemplate will hopefully help you answer the following:
StringTemplate (or ST) is a template engine library used for generating text from data structures. The first version was developed for the Java platform and has evolved over the years of it's use on the jGuru.com website. StringTemplate's distinguishing characteristic is that it strictly enforces model-view separation unlike other comparable template engines. It is particularly good at multi-targeted code generators, multiple site skins, and internationalization/localization.
Independent implementations of some versions of StringTemplate are available in C# and Python (discussions about Objective-C and C++ implementations of StringTemplate occur frequently on the mailing list). StringTemplate is currently used to generate the ANTLR website and the StringTemplate website. It is also the engine that powers the powerful, flexible, multi-language, retargetable code generator in the ANTLR v3 language tool generator.
Structured text generation. StringTemplate supports the generation of structured text output such as web pages, source code, emails and newsletters from arbitary data structures without sacrificing model-view separation.
StringTemplate is non-intrusive and doesn't require your data structures to implement any specific contracts. It is designed to be embedded inside other applications and is distributed as a small library with no external dependencies except ANTLR (used for parsing the StringTemplate template language) and, the standard platform libraries (e.g. JDK 1.2 for the Java version).
To ensure the separation of the specification of business logic and computation required to generate structured text from the specification of how that text is presented (i.e. it's formatting).
For web developers, this translates to ensuring that a web page's business and persistence logic is separated from the the page's presentation.
For developers involved in code generation, this translates to ensuring generation and persistence logic is separated from output code structure. This separation directly supports the development of retargetable code generators.
Other benefits of model-view separation:
Download and install StringTemplate from the download page on the StringTemplate website
Please note that:
StringTemplate
supports the use of <
...>
and $
...$
as delimiters (as shown in the examples)Syntax |
Description |
Example |
---|---|---|
<attribute> |
Replaced with value of attribute |
|
<attribute.property> |
Replaced with value of property of attribute (or empty string if missing). |
|
<attribute.(expr)> |
Indirect property lookup. Same as attribute.property except value of expr is the property name. |
|
<multi-valued-attribute> |
Concatenation of |
|
<multi-valued-attribute; separator=expr> |
Concatenation of |
|
<template(argument-list)> |
Include (i.e. call) template. argument-list is a list of attribute assignments of the form arg-of-template=expr. expr is evaluated in the context of the surrounding template not of the invoked template. |
|
<attribute:template(argument-list)> |
Template application. The optional argument-list is evaluated before application. The default attribute it is set to the value of attribute. If attribute is multi-valued, it is set to each element in turn and template is invoked |
|
<attribute:{argument-name_ | _anonymous-template}> |
Apply an anonymous template to each element of attribute. Set the argument-name to the iterated value and also set |
|
<if(!attribute)>subtemplate<endif> |
If attribute has no value or is a |
|
|
escaped delimiter prevents |
|
|
special characters: space, newline, tab, carriage return. |
|
|
Comments, ignored by StringTemplate. |
|
Where better to start than a StringTemplate version of the ubiquitous "Hello, World" example...
Starting with version 2.3, StringTemplate
uses <
..>
as it's default delimiters. For completeness, we will use $
..$
here to illustrate how specific delimiters are specified with StringTemplate
.
Java |
|
|
---|---|---|
C# |
|
|
Python |
|
|
Python |
|
First, create the homepage.st
template file as shown below and save it to a location on your system.
|
Next, create the code that loads and uses the template file. To do so, we will make use of the StringTemplateGroup
class that manages external templates.
java |
|
|
---|---|---|
C# |
|
|
Python |
|
|
Python 3 |
|
Read the StringTemplate Documentation
Browse the list of questions frequently asked about StringTemplate
If this guide has been helpful to you, please consider contributing to this wiki when you are more familiar with StringTemplate.
Thanks