${import}

<${switch} Statement  

The ${import} statement

Being able to expand a template is nice, but you will soon reach the point where you would like to compose the output from pieces that each have their own, limited areas of responsibility. For example, a generated source code file might be composed from a generic preamble, a class declaration, and several nested elements. Each of these elements could reside in its own template.

Here is an example of a class file template:

class.tpl

${import('preamble.tpl')}
package ${class.package};

public class ${class.name} extends ${class.superclass.fullname} {
${foreach(ctor:class.ctors) #indent+=4}
${import('constructor.tpl')}
${end}

${foreach(method:class.methods) #indent+=4}
${import('method.tpl')}
${end}
} 

This is an example of a source code file preamble template. It includes terp expressions that cause an accurate copyright statement and other build information to be included:

preamble.tpl

// Copyright (c) 2008-${now['%tY']} by Codemesh, Inc.
// ALL RIGHTS RESERVED.
//
// This file was generated on ${now} by ${generator.user}.
// It is part of the  ${product.version} release. 

The constructor template relies on the fact that the ctor and class objects are defined. The class.tpl template provides definitions for both variables.

constructor.tpl

(  ) {
    // the constructor contents 
}

There's a certain amount of pseudo-code in these examples, but you probably get a pretty good idea of how you can use, and reuse, templates by importing them into parent templates.


Copyright 2006-2016 by Codemesh, Inc., ALL RIGHTS RESERVED

:
codemesh.com home expressions templates ant about us contact us download   

Commandline