%string AString = "Some value" %output AString $ AString is cleared before %outputting (no %append) $ Note here an implicit %pushindent 0 is executed, followed by a %indent 0 ... block of code that generates something... $ Here an implicit %popindent is executed %endoutput
%output %append AString $ AString is appended with the new value $ Implicit %pushindent 0 and %indent 0 as above ... block of code that generates something... $ Implicit %popindent %endoutput
%output %keepindent AString $ AString is cleared before %outputting (no %append) $ No %pushindent 0/%indent 0 executed due to %keepindent ... block of code that generates something... $ No %popindent executed here (%keepindent) %endoutput
The %output statement works exactly like a %file statement, but generated output between %output and %endoutput are written to the string specified as %output argument. %output can write to a member property as well, for example, %output Self->MyProp.
The optional %append clause appends output contents to the given string (default is flushing the string contents when the %output statement is executed).
There is no limitation on what the code block between %output and %endoutput can contain: loops, %load and operation calls are allowed (exactly like a %file statement situation).
The following example captures the operation implementation from all the model classes reusing the OperationImpl.sdl module, then appends a comment, then appends a module footer by calling some operation. In the end, the string is printed to a file:
%localstring A %output "A" %for %search ("", "Class") %load "OperationImpl.sdl" %endfor %endoutput $Append a comment A = A & "\n// This is a comment\n" $Append a footer %output %append "A" GenerateFooter () %endoutput
%file "OutputFile.cpp" A %endfile
By default, %output executes an implicit %pushindent 0 followed by a %indent 0. This is consistent with subsequent use of the generated string in other %output or %file statements, in such a way the indent settings of the second %output is not summed with the previous. For example:
%indent 1 %output X "if (A < B) {\n" %pushindent 1 "A = B;\n" %popindent "}\n" "return A;\n" %endoutput %output Y "int SelectGreater (int A, int B) {\n" %pushindent 1 X $ outputs the value captured with previous %output %popindent "}\n" %endoutput $ Here the %indent level is still 1 %file "MyFile.cpp" Y %endfile
The above generates the following:
int SelectGreater (int A, int B) { if (A < B) { A = B; } return A; }
Note the initial indent level (1) is ineffective on both %output statements, which restart with a 0 indent. Also note this behavior is consistent: if the %output statements did not reset the indent, the result would have been as follows:
int SelectGreater (int A, int B) { if (A < B) { A = B; } return A; }
This because the X block would have been indented twice (at first %output statement, then at second %output statement when the whole X is %outputted again).