Book a Demo
Prev Next

Code and Transform Templates

Code templates and transform (Model Transformation) templates define how the system should generate or transform code in one or other of the programming languages that Enterprise Architect supports. Each language has a wide range of base templates, each of which defines how a particular code structure is generated. You can use these base templates as they are, or you can customize and add to the templates to better support your use of the standard languages, or of other languages that you might define to the system. You review, update and create templates through the Code Template editor or Transformation Template editor.

The order in which the base templates are listed in the two editors relates to the hierarchical order of the objects and their parts that are to be processed. Calls are made from certain base templates to others, and you can add further calls to both base templates and to your own custom templates. By default, the File template is the starting point of a code generation process through the templates; a File consists of Classes that can contain Attributes and Operations.



Develop >Source Code > Options > Edit Code Templates

Design > Package > Transform > Transform Templates

Keyboard Shortcuts

Ctrl+Shift+P (Code Generation Templates)

Ctrl+Alt+H (MDA Transformation Templates)

Application of Templates



See also

Calling Templates

Within any template, you can call other templates using %TemplateName%. The enclosing percent (%) signs indicate a macro.

You would use this for a single call to the ClassBody template, %ClassBody%, as shown:

     % list = "TemplateName" @separator= "\n" @indent= "  " %

The %list macro performs an iterative pass on all the objects in the scope of the current template and calls the TemplateName for each of them:

     % list = "ClassBody" @separator= "\n" @indent= "  " %

After generation or transformation, each macro is substituted to produce the generated output; for a language such as C++, the result of processing this template might be:


     * This is an example Class note generated using code templates

     * @author Sparx Systems


     class ClassA: public ClassB




List Macro

Execution of Code Templates

Each template might act only on a particular element type; for example, the ClassNotes template only acts on UML Class and Interface elements.

The element from which code is currently being generated is said to be in scope; if the element in scope is stereotyped, the system searches for a template that has been defined for that stereotype. If a specialized template is found, it 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.

Transfer Templates Between Projects

If you edit a base Code Generation or Transformation template, or create a customized template, you can copy them from one project to another as Reference Data.

Export Code Generation and Transformation Templates Import Code Generation and Transformation Templates

Learn more