Please note : This help page is not for the latest version of Enterprise Architect. The latest help can be found here.

Code Templates




See also


Code templates enable you to customize code generation of existing languages. For example:

  • Modify the file headers created when generating new files
  • Change the style of the generated code (such as indenting or brace position) to match the required coding standards
  • Handle particular stereotypes to generate things like specialized method bodies and extra methods

They also enable you to add code generation of entirely new languages that Enterprise Architect would otherwise not be able to handle. In this situation it is most useful to combine code templates with an MDG technology file that includes the datatypes, and options for default file extensions.

Enterprise Architect's base code templates specify the transformation from UML elements to the various parts of a given programming language. The templates are written as plain text with a syntax that shares some aspects of both mark-up languages and scripting languages. A simple example of a template used by Enterprise Architect is the 'Class template'. It is used to generate source code from a UML Class:




The above template simply refers to three other templates, namely ClassNotes, ClassDeclaration and ClassBody. The enclosing percent (%) signs indicate a macro. Code Templates consist of various types of macros, each resulting in a substitution in the generated output. For a language such as C++, the result of processing the above template might be:


* This is an example class note generated using code templates

* @author Sparx Systems


class ClassA: public ClassB




Add Code Modules

Base Templates

Execution of Code Templates

A reference to a template (such as the %ClassNotes% macro, from our example above) results in the execution of that template.

Each template is designed for use with a particular element. For example the ClassNotes template is to be used with UML Class elements.

The element that is currently being generated is said to be in scope. If the element in scope is stereotyped Enterprise Architect looks for a template that has been defined for that stereotype. If a match is found, the specialized template is executed. Otherwise the default implementation of the base template is used.

Templates are processed sequentially, line by line, replacing each macro with its underlying text value from the model.