Using New Lines in Head and Tail String

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.

Example

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

Additional Example

Consider a class named Nurse, with a class code Nurse, and two attributes:

Attribute name

Data type

Initial value

NurseName

String

__

NurseGender

Char

'F'

The following templates are given as examples, together with the text generated for each of them, and a description of each output:

Template 1

class "%Code%" {
 // Attributes
 .foreach_item(Attributes)
 %DataType% %Code%
  .if (%InitialValue%)
 = %InitialValue%
  .endif
 .next
 // Operations
 .foreach_item(Operations)
 %ReturnType% %Code%(...)
 .next
}

Text Generated 1

class "Nurse" {
 // Attributes String nurseName char nurseGender = 'F' // Operations}

Description 1

Below the class code, the code is generated on one line. It is an example of a block macro (.if, .endif macro).

Template 2 (new Line)

class "%Code%" {
 // Attributes
 .foreach_item(Attributes)
 %DataType% %Code%
  .if (%InitialValue%)
 = %InitialValue%
  .endif
 .next(\n)
 // Operations
 .foreach_item(Operations)
 %ReturnType% %Code%(...)
 .next(\n)
}

Text Generated 2

class "Nurse" {
 // Attributes String nurseName
 char nurseGender = 'F' // Operations}

Description 2

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).

Template 3 (blank Space)

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)
}

Text Generated 3

class "Nurse" {// Attributes
 String nurseName
 char nurseGender = 'F'
}

Description 3

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.

Template 4 (blank Space)

class "%Code%" {\n
 .foreach_item(Attributes," // Attributes\n",\n)
 %DataType% %Code%[ = %InitialValue%]
 .next(\n)
 .foreach_item(Operations," // Operations\n",\n)
 %ReturnType% %Code%(...)
 .next(\n)
}

Text Generated 4

class "Nurse" {
 // Attributes
 String nurseName
 char nurseGender = 'F'
}

Description 4

The double quote characters ("") in " // Attributes\n" allows you to insert a blank space as shown in the output: // Attributes

Note: 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