include

Syntax

[#include templateName(arguments)]

where:

Description

The include directives causes the referenced text template to be evaluated and the resulting text to be inserted at this position of the include directive in the current template. Note that if the referenced text template uses a file directive, this file will be ignored.

The template may be referenced using a qualified name (dotted notation including the package name) or using a simple name. When a simple name is used, the referenced template is first searched in the caller template package. If the template is not found, then it is resolved using the import directives of the caller template.

The arguments must match the referenced template parameters, in the order they are defined.

The include directive may be used at any level of a text template contents, as well as inside a TGL script.

Examples

Simple Copyright example

This template defines a copyright header to be included (does not contain a file directive) :

[#package com.mycompany.example]

[#template Copyright()]
/**
 * Copyright 2006 My Company, Inc. All rights reserved.
 */
[/#template]

This template includes the copyright header :

[#package com.mycompany.example]

[#template GenerateJavaClass(class : uml21.Class)]
[#file]${class.name}.java[/#file]
[#include Copyright()] [#-- simple name, as the Copyright template is in the same package --]
public class ${class.name} {
}
[/#template]

The above template would produce an output similar to the following:

/**
 * Copyright 2006 My Company, Inc. All rights reserved.
 */
public class Account {
}

Qualified include example

This template outputs the name of a UML element :

[#package com.mycompany.utils]

[#template ElementDeclaration(element : uml21.NamedElement)]
    name: ${element.name}
[/#template]

This template iterates on a UML package contents :

[#package com.mycompany.example]

[#template PackageDeclaration(package : uml21.Package)]
Package contents:
[#foreach ownedElement in package.ownedElement]
    [#-- qualified name, as the ElementDeclaration template is not in the same package --]
    [#include com.mycompany.utils.ElementDeclaration(ownedElement)]
[/#foreach]
[/#template]

The above template would produce an output similar to the following:

Package Contents:
    name: Account
    name: Customer
    name: Bank

The PackageDeclaration template could be rewritten to use a simple name reference by defining an import:

[#package com.mycompany.example]

[#import com.mycompany.utils.ElementDeclaration]

[#template PackageDeclaration(package : uml21.Package)]
Package contents:
[#foreach ownedElement in package.ownedElement]
    [#-- simple name, as the ElementDeclaration template is imported --]
    [#include ElementDeclaration(ownedElement)]
[/#foreach]
[/#template]