The head and tail string are only generated when necessary. If no code is generated, the head and tail strings do not appear. This can be useful when controlling new lines.
You want to generate the name of a class and its attributes under the following format (one empty line between attributes and class):
Attribute 1 attr1 Attribute 2 attr2 Class
You can insert the separator "\n" after the .foreach statement to make sure each attribute is displayed in a separate line. You can also add "\n\n " after the .endfor statement to insert an empty line after the attribute list and before the word "Class".
.foreach (Attribute) ("\n") Attribute %Code% .endfor ("\n\n") Class
Consider a class named Nurse, with a class code Nurse, and two attributes:
The following templates are given as examples, together with the text generated for each of them, and a description of each output:
class "%Code%" { // Attributes .foreach_item(Attributes) %DataType% %Code% .if (%InitialValue%) = %InitialValue% .endif .next // Operations .foreach_item(Operations) %ReturnType% %Code%(...) .next }
class "Nurse" {
// Attributes String nurseName char nurseGender = 'F' // Operations}
Below the class code, the code is generated on one line. It is an example of a block macro (.if, .endif macro).
class "%Code%" { // Attributes .foreach_item(Attributes) %DataType% %Code% .if (%InitialValue%) = %InitialValue% .endif .next(\n) // Operations .foreach_item(Operations) %ReturnType% %Code%(...) .next(\n) }
class "Nurse" {
// Attributes String nurseName
char nurseGender = 'F' // Operations}
String nurseName and char nurseGender are on two lines
In Template 1, String nurseName and char nurseGender were on the same line, whereas in Template 2, the addition of the \n at .next(\n) puts String nurseName and char nurseGender on two different lines.
In addition, // Operations is displayed in the output even if there is no operation (see Description 3).
class "%Code%" { .foreach_item(Attributes, // Attributes\n,\n) %DataType% %Code% .if (%InitialValue%) = %InitialValue% .endif .next(\n) .foreach_item(Operations, // Operations\n,\n) %ReturnType% %Code%(...) .next(\n) }
The blank space between .foreach_item(Attributes, and // Attributes\n,\n) is not generated, as shown in the output: class "Nurse" {// Attributes instead of .... { // Attributes
// Operations is not displayed in the output because it is positioned in the .foreach_item macro. It is positioned in the head of the macro for this purpose.
class "%Code%" {\n .foreach_item(Attributes," // Attributes\n",\n) %DataType% %Code%[ = %InitialValue%] .next(\n) .foreach_item(Operations," // Operations\n",\n) %ReturnType% %Code%(...) .next(\n) }
The double quote characters ("") in " // Attributes\n" allows you to insert a blank space as shown in the output: // Attributes
The newline immediately preceding a macro is ignored as well as the one immediately following it, as in the following example:
Jack .set_value(v, John) Paul yields: JackPaul
instead of: Jack Paul