The CodeRush templates engine allows you to set global variables and insert their values into any other template. This feature is especially useful when expanding a template within another template. Variables and the ability to expand a template within another template enable you to extract a part of a template and insert that part into several other templates.
Text commands considered in this article are presented below.
- To set a variable, use the «?Set(variable_name,value)» text command. This text command produces no output.
- To insert the variable contents, use the «?Get(variable_name)» text command.
- To expand another template, use the «:template_name» text command.
For example, you can define the following template. It inserts the declaration and initialization of the new SQL connection.
Sub-templates should be given long descriptive names, which end-users would unlikely type. The sub-templates from the templates library are additionally surrounded with the hash (#) sign.
After defining the sub-template, you can use it as follows. Set the variables referenced by the sub-template, then place the text command expanding it.
The template presented above will expand as follows.