How to Customize Code Generation Templates?
Instant generator allows you to generate programming source code from class models. Basically, the content of the generated code follows the common coding convention of the programming language. There are also advanced options for you to configure some of the specific settings in forming the code, like the use of prefix for attributes and parameters.
Although the built-in way of generating source code can satisfy most of the general needs, you may want to define something more specific. For example, you may need to print a copyright statement at the beginning of the code file, which is not a kind of customization being supported by Instant generator.
Fortunately, the way of how source code will be generated is handled by Apache Velocity engine, a templating engine, and the templates being used are fully opened for customization. In the following sections, we will explain how to customize a template to make the generated code follow your requirement.
Setting up development environment
The template files are put under the resources/instantgenerator folder of Visual Paradigm installation directory. It is absolutely alright to edit those files directly. However, it is recommended to setup your own development environment, copy the template files to there to perform further editing. There are two reasons for separating the development environment from Visual Paradigm:
- Avoid the unexpected template removal by un-installing the Visual Paradigm.
- Avoid accidental file replacement by running product updates.
To setup your development environment:
- Create a folder as working directory.
- Explore %Visual-Paradigm-Installation-Directory%/resources/instantgenerator.
- You will see a number of sub-folders that have the programming language as their names. Each of them contains the templates files for a specific programming language. Copy the folder(s) of the language(s) you need to customize and paste to the working directory.
By having the text editor and the development environment ready, it's time to get your hand dirty with editing the template. As mentioned before, Instant generator adopted the Apache Velocity engine in generating source code. For those who are interested in knowing how to write templates, please read Velocity's Users' guide at:
The following example demonstrates how to edit the PHP code generation template to reposition the brace of operation blocks to a new line.
|Customization of operation in PHP class
- Open the template you need to edit in text editor.
Open PhpOperation.vm in text editor
- Look for the area that you need to edit.
Search for the open branch
- Make change.
Insert line breaks
- Add a variable $indentation to indicate the need of printing indentation before the open brace.
- Save the file.
Generate code with the customized template
To generate code with customized template:
- In Visual Paradigm, select Tools > Code > Instant Generator from the toolbar, then the programming language that have the template customized.
- Specify the Template directory where the customized templates are stored.
Specifying template directory
- Select the classes to generate. Specify the output path. Click Generate to generate code. You may refer to previous chapters for details about instant generator.
List of API calls
The following table lists the available API calls for retrieving data from models.
|A list of API calls
The following lists the syntax that of statements that can be used in the template.
## ===== If-then-Else =====
## ===== For-each =====
## ===== Continue with the template defined in (...) at the point where the call is made =====
## ===== Comment =====
## ===== Comment =====
#* ... *#
## ===== Variable=====
The following resources may help you to learn more about the topic discussed in this page.
|16. Instant Generator for Ruby||Table of Contents||Chapter 3. Java Round-Trip|