Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

CodeSmith uses a master-from-code template


May 25, 2021 CodeSmith


Table of contents


Use the master-from code template

In the previous tutorial CodeSmith used tutorial (3): Auto-generating Yii Framework ActiveRecord We used the main, from the template to implement the ActiveRecord class definition that generates multiple tables from the database for Yii Framework, where the CodeSmith project enables a complex code generation process through the master template and the mate from the template. T he relationship between the main template and the from the template is somewhat similar to that between the main program and the child function. The basic steps for using the master-from template are as follows:

  • Defined from a template, from which you can define properties
  • Define the main template, in which if you want to use the from template, you first need to register from the template in the main template, and you can also define properties in the main template, and the main template and properties from the template can construct the collection of properties defined by the final template by defining the Merge pattern.
  • Call the main template, set the main template, and generate the required code from the properties required by the template

Register sub-templates

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

Name: The type name of the sub-template in the main template, in which you can create an instance template of the sub-template from that type Template: The sub-template file name MergeProperties: Whether you need to incorporate the properties defined in the sub-template into the main template. The default is False ExcludeProperties: If the properties of the sub-template are merged into the main template, the list of properties that need to be excluded is separated by commas.

The sub-template copies the properties in the main template

MergeProperties "True" you can merge properties from a template into the main template, and if you need to reference a property of the main template from a template, such as a server address defined in the main template, and you need to reference this property in multiple sub-templates, you can do so by copying the parent template property CopyPropertiesTo:

// instantiate the sub-template
Header header = this.Create<Header>();

// copy all properties with matching name and type to the sub-template instance
this.CopyPropertiesTo(header);

The CopyPropertiesTo method compares the properties defined in the main template with property defined in the sub-template, and copies the property values in the main template to the sub-template if the properties defined from the template are found to have the same type of property name (match) as defined from the main template.

Set sub-template properties

To create an instance of a sub-template in the main template, you can do so directly through the Create method

// instantiate the sub-template
Header header = this.Create<Header>();

// include the meta tag
header.IncludeMeta = true;

Header in Create is a type defined by Name when registering a sub-template, and when an instance of a sub-template is created through Create, properties in the sub-template can be accessed directly through the properties of that instance, such as a property defined in the sub-template by IncludeMeta in the code above.

Output results from sub-templates

Create an instance of the sub-template, set the properties of the sub-template, in the main template can let the sub-template output results, there are several ways to output content from the sub-template.

The first is to insert the results generated by the sub-template directly into the main template

// instantiate the sub-template.
Header header = this.Create<Header>();
// render the sub-template to the current output stream.
header.Render(this.Response);

The second method is to output the results to a separate file:

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

Examples can be found in the CodeSmith Usage Tutorial (3): Auto-generating Yii Framework ActiveRecord