madhavrao… » T4 - Directives & Directive Processors

T4 - Directives & Directive Processors

Directives and directive processors provide additional functionality to text templates. This functionality can be a simple as the ability to specify the extension of the output file or as complex as custom directive processors that read data from a database.

A directive processor contains one or more directives, and the same directive processor can process more than one directive. As an analogy, you can think of directive processors as classes, and you can think of directives as methods in those classes. You call directives directly from your text templates.

Example of Directives are:

  1. output: this is used to specify extension of the file.
  2. template: this is used to specify various properties of the template, one of which is language used to write control logic.

Directives can be classified into three types:

  1. Built-In Directives
  2. Generated Directives
  3. Custom Directives

Built-In Directives

Built-in directives are provided by the text template transformation toolkit. By using built-in directives, you can specify common options such as the programming language of the text template and the extension of the output file. There are five built-in directives: assembly, import, template, output, and include.

Generated Directives

Generated directives are based on the domain-specific language that you create. If you call a generated directive, you can access models from the statements and expressions in text templates.

Custom Directives

One can write custom directive processors to provide custom functionality to text templates. Custom directive processor can be used any time that one want to access external data or resources from a text template.

Different text templates can share the functionality that a single directive processor provides, so directive processors provide a way to factor code for reuse. The built-in include directive is similar, because you can use it to factor out code and share it among different text templates. The difference is that any functionality that the include directive provides is fixed and does not accept parameters. If you want to provide common functionality to a text template and allow the template to pass parameters, you must create a custom directive processor.

Some examples of custom directive processors could be:

  1. A directive processor to return data from a database that accepts a user name and password as parameters.
  2. A directive processor to open and read a file that accepts the name of the file as a parameter.

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <blockquote cite=""> <code> <em> <strong>