Dec 16, 2007

Sub-Template

To register a sub-template, you include a register directive in the master template. You can include as many Register directives as you like, so one master template can include multiple sub-templates. Sub-templates can be nested.

            <%@ Register Name="Header" Template="Header.cst" MergeProperties="True"
            ExcludeProperties="IncludeMeta" %>
            

The MergeProperties attribute specifies whether the properties of the sub-template should be dynamically added to the master template's properties. If you omit this attribut, it default to false. When you merge the properties, the properties of the sub-template will be displayed on the property sheet of the main template when the main template is open in codesmith explorer or codesmith studio. This makes it easy to prompt for all the properties that are required for the entire code-genertion process on a single propery sheet.

You may want to share properties between a master template and sub-templates. Suppose your master template and sub-template both have a string property named "Server", when your prompt for this property in the master template, only the master template's copy of the property receives a value. To set the property in the sub-template, you use the CopyPropertiesTo method of the master template. This method matches properties from the master template to the sub-template on the basis of name and type. If it finds an exact match, it copies the value from the master template to the sub-template.

            //instantiate the sub-template
            Header header - this.Create
(); this.CopyPropertiesTo(header);

If you want to specify a property individually, you can use the following code.

            Header header = this.Create
(); //this is strongly typed template, so that you can use its tempalte directly. header.IncludeMeta = true;

However, you don't need to register a template if you don't need to create a strongly type sub template.

            CodeTemplate genericTemplate = this.GetCodeTemplateInstance("sub.cst");
            genericTemplate.SetProperty("TemplateName", "Sub template");
            

After you've registered a sub-template and set its properties, you can render the sub-template. You can render the sub-template directly to the output of the main template

            Header header = this.Create
(); header.Render(this.Response);

Alternatively, you can render the sub-template to a seperate file. This is useful when you want to create multiple output files as part of a single code-generation process.

            Header header = this.Create
(); header.RenderToFile("SomeFile.txt");

Another way to to use sub-template is to render sub-template to a seperate file. This is useful when you want to create multiple output files as port of single code-generation process.

            // instantiate the sub-template
            Header header = this.Create
(); // render the sub-template to a separate file header.RenderToFile("Somefile.txt");

If main template and sub-template share the same base template and main template has all the properties that sub-template has, you don't need to register the sub-template simply because you want to initialized properties in sub-template. All you need to copy the by using "this.CopyPropertiesTo(header);". However if the main template does not have all the properties that sub-template has , you need to register the sub-template , so that use can fill in the properties of sub-template in the main template.