From RTSC-Pedia
revision tip
LANDSCAPE orientationXDCscript - Template-Body
Contents of the template file
Description
The body of a template contains lines of text that are automatically output when the template object is expanded via genStream() or genFile(). It also contains lines of script that are evaluated prior to generating output. This script code can reference any object in the current environment — with $out, this, and $args bound to parameters of genStream().
- this
- an arbitrary object that is passed in by the caller; in the case of templates specified by the @Template attribute, this is the module object associated with the module that names this template via @Template.
- $args
- array of arguments passed in by the caller; in the case of templates specified by the @Template attribute, $args has just one element which is either the absolute path or a package path relative path of the template file itself.
- $out
- a java.io.PrintStream where all output is written; in the case of templates specified by the @Template attribute, $out is the output stream of the generated configuration .c output file.
In addition to the variables above, templates are expanded in the context of some domain model and the global objects associated with this model are also available. For example, templates specified by the @Template attribute are expanded in the context of the Configuration Domain and, as a result, the Program object can be used to affect the generated output.
Syntax
- text`script`text ... \n
- A line of text to be output by the template at this point. If present, all script between and including the `` quotes is replaced with its (textual) value in the current context.
- % script ...\n
- A single line of script evaluated by the template at this point.
- %%{\n
- stmt ...\n
- stmt ...\n
- %%}\n
- Multiple lines of script to be evaluated at this point.
Example
The following script and template illustrate many of the capabilities of templates. This example generates a copyright header from a template.
| gen.xs |
| var template = xdc.loadTemplate("copyright.xdt");
template.genStream(java.lang.System.out, this,
["TI", arguments]); |
| | copyright.xdt |
| %%{
var NAME = $args[0];
var CTRB = $args[1];
var DATE = new Date().getFullYear();
%%}
/* Copyright `DATE` by `NAME`.
* All rights reserved. Property of `NAME`.
* Contributions from:
%for (var i = 0; i < CTRB.length; i++) {
* `CTRB[i]`
%}
*/ |
|
Running this script produces the following output.
| xs -f gen.xs bob carol ted alice |
| /* Copyright 2008 by TI.
* All rights reserved. Property of TI.
* Contributions from:
* bob
* carol
* ted
* alice
*/ |
See also
Copyright © 2008 The Eclipse Foundation. All Rights Reserved