Chapter 9 Generation Reference Guide (GTL)


Using parameter passing

You can pass in, out or in/out parameters to a template through local variables by taking advantage of nested translation scopes. You can access parameters with the %@<number>% variable.

Example

Class templates:

Template 1

<show> template
<<<
Class "%Code%" attributes :
// Public
%publicAttributes%

// Protected
%protectedAttributes%

// Private
%privateAttributes%
>>>

Template 2

<publicAttributes> template
<<<
.foreach_item(Attributes)
 .if (%Visibility% == +)
 %DataType %Code%
 .endif
.next(\n)
>>>

Template 3

<protectedAttributes> template
<<<
.foreach_item(Attributes)
 .if (%Visibility% == #)
 %DataType %Code%
 .endif
.next(\n)
>>>

Template 4

<privateAttributes> template
<<<
.foreach_item(Attributes)
 .if (%Visibility% == -)
 %DataType %Code%
 .endif
.next(\n)
>>>

To give you more readability and to enhance code reusability, these four templates can be written in just two templates by using parameters:

First template

<show> template
<<<
Class "%Code%" attributes :
// Public
%attributes(+)%

// Protected
%attributes(#)%

// Private
%attributes(-)%
>>>

Second template

<attributes> template
<<<
.foreach_item(Attributes)
 .if (%Visibility% == %@1%)
 %DataType %Code%
 .endif
.next(\n)
>>>

Description

The first parameter in this example %attributes(+, or #, or -)% can be accessed using the variable %@1%, the second parameter when it exists, is accessed using the %@2% variable, etc ...

 


Copyright (C) 2005. Sybase Inc. All rights reserved.