Template Tags

The code generator processes the following template tags inside the templates to generate the working code, intermediate or advanced users can make use of these tags to create custom templates. All template tags have the same basic syntax:

<# ... #>

It is important to understand how templates tags works. During code generation, the code generator reads the template file, executes the scripts in the template tags, and write the code to the output file.

 

1. Script Tag

To generate code according to the project settings, there are many script blocks in template. Each script block is enclosed by special script tags.

Syntax:

<#
...Script here...
#>

The scripting language used in the script block is JavaScript as supported by Node.js. To customize template, you need to know JavaScript.

In a script block, you can create variables and constants, use conditional statements, do looping, write functions, everything you can do with JavaScript. You can also access all settings in the project. (See Object Properties for details.) You can generate virtually any code you want.

Example 1

Only generate some code if the table is a view. Here TABLE is a template object and TblType is one of its properties.

<# if (TABLE.TblType == "VIEW") { #>
...code here...
<# } #>

Example 2

Add a local function for later use.

<#
function MyFunction() {
    return "This is my custom function";
}
#>

 

2. Output Tag

Output tag outputs a string (which is the generated code), e.g. a literal string, an object property, output by function, etc.

Syntax:

<#= ... #>

Note The = symbol is immediately after the start tag <#.

Or you can use the built-in write() function:

<# write("...") #>

Example 1 - Output literal string

<#= "something" #>

<# write("something") #>

You can also use JavaScript Template literals, e.g.

<#= `string text line 1
string text line 2` #>

<# let value = "thing" #>

<#= `some${value}` #>

Example 2 - Output object property

<#= PROJ.ProjName #>

This line will write the project name in the output file.

Example 3 - Output function result

<#= MyFunction() #>

 

3. Include function

A template file usually includes many sub-templates. To include a sub-template, use the built-in include() function:

<# include("file.ext") #>

Example

A sub-template can be in the same folder of the main template file or in a sub-folder in the template:

<# include("xxx.html") #>
<# include("shared/yyy.html") #>

 

Also See

Object Properties
Using User Code

 

 ©2016-2021 e.World Technology Ltd. All rights reserved.