Code Templates are written as plain text, using Enterprise Architect's code template editor (see The Code Template Editor ). The template syntax centers on three basic constructs:
Templates can contain any or all of these constructs.
All text within a given template that is not part of a macro or a variable definition/reference, is considered literal text. With the exception of blank lines, which are ignored, literal text is directly substituted from the template into the generated code.
Consider the following excerpt from the java Class Declaration template:
%PI=" "%
%CONVERT_SCOPE(classScope)%
%classStereotype=="static" ? "static": ""%
%classStereotype=="final" ? "final": ""%
%classStereotype=="static final" ? "static final": ""%
%classAbstract=="T" ? "abstract": ""%
%PI=""%
class %className%$bases
On the final line, the word class , including the subsequent space, would be treated as literal text and thus reproduced in the output. The blank line following the CONVERT_SCOPE macro, however, would have no effect on the output.
The %, $ and " characters have special meaning in the template syntax and cannot always be used as literal text. If these characters must be generated from within the templates, they can be safely reproduced using the following direct substitution macros:
Macro |
Use to |
%dl% |
Produce a literal $ character. |
%pc% |
Produce a literal % character. |
%qt% |
Produce a literal " character. |
Macros provide access to element fields within the UML model and are also used to structure the generated output. All macros are enclosed within percent ( % ) signs. The CTF contains six basic types of macros:
In general, macros (including the % delimiters) are substituted with literal text in the output. For example consider the following item from the Class Declaration template:
... class %className% ...
The field substitution macro, %className% , would result in the current Class name being substituted in the output. So if the Class being generated was named Foo , the output would be:
... class Foo ...
Template variables provide a convenient way of storing and retrieving data within a template. This section explains how variables are defined and referenced .
Variable definitions take the basic form:
$<name> = <value>
where <name> can be any alpha-numeric sequence and <value> is derived from a macro or another variable.
A simple example definition would be:
$foo = %className%
Variables can be defined, using values from:
Definition Rules
The following rules apply to variable definitions:
Examples
Using a substitution macro:
$foo = %opTag:"bar"%
Using a literal string:
$foo = "bar"
Using another variable:
$foo = $bar
Using a list macro:
$ops = %list="Operation" @separator="\n\n" @indent="\t"%
Using the addition-assignment operator ( += ):
$body += %list="Operation" @separator="\n\n" @indent="\t"%
The above definition is equivalent to the following:
$body = $body + %list="Operation" @separator="\n\n" @indent="\t"%
Using multiple terms:
$templateArgs = %list="ClassParameter" @separator=", "%
$template ="template<" + $templateArgs + ">"
Variable values can be retrieved by using a reference of the form:
$<name>
where <name> can be a previously defined variable.
Variable references can be used in one of the following ways:
Note: |
It is legal to reference a variable before it is defined. In this case, the variable is assumed to contain an empty string value: "" |
Example 1
Using variables as part of a macro. The following is an excerpt from the default C++ ClassNotes template.
$wrapLen = %genOptWrapComment% $style = %genOptCPPCommentStyle%
%if $style == "XML.NET"% %XML_COMMENT($wrapLen)% %else% %CSTYLE_COMMENT($wrapLen)% %endIf% |
Define variables to store the style and wrap length options. Reference to $style as part of a condition. Reference to $wrapLen as an argument to function macro. |
Example 2
Using variable references as part of a variable definitions:
$foo = "foo" $bar = "bar"
$foobar = $foo + $bar |
Define our variables. $foobar now contains the value foobar. |
Example 3
Substituting variable values into the output
$bases=%classInherits%
Class %className%$bases |
Store the result of the ClassInherits template in $bases. Now output the value of $bases after the Class name. |