Codesmith Use Tutorial (12) using master-slave code template __codesmith

Source: Internet
Author: User
Tags class definition list of attributes yii


In the previous tutorial Codesmith tutorial (3): Automatically generate the YII framework ActiveRecord we used the master, from the template to implement the ActiveRecord class definition that generates multiple tables from the database for the YII framework, In the Codesmith project, the complex code generation process can be achieved through the combination of the master template and the template, and the relationship between the master template and the template is somewhat similar to that of the main program and the child function. The basic steps for using the master-from template are as follows: definition from templates, from templates can define a property definition master template, in the main template if you want to use from the template, you need to register the template in the main template, the main template can also define properties, the main template and properties from the template can be defined by the "merge" Pattern constructs a collection of attributes defined by the final template. Invoke the main template, set up the main template and generate the required code from the properties required by the template

Registering a child template

[CSharp] view plain copy print? <%@ Register name= "Header" template= "HEADER.CST" mergeproperties= "True" excludeproperties= "Includemeta"%>

<%@ Register name= "Header" template= "HEADER.CST"
  mergeproperties= "True" excludeproperties= "Includemeta"% >

Name: The type names of the child templates in the main template, where you can create instances of the child templates from that type
Template: Child template file name
Mergeproperties: Whether you want the attributes defined in the handle template: Merge to the main template. Default is False
Excludeproperties: A comma-delimited list of attributes that you want to exclude when the properties of the child template are merged into the main template.

Child templates replicate properties in the main template
Mergeproperties= "True" can incorporate attributes from a template into the main template, and if you need to refer to the properties of the main template from the template, such as the server address defined in the main template, you need to reference this attribute in multiple child templates. This can be done by copying the parent template property Copypropertiesto: [CSharp] view plain copy print? Instantiate the Sub-template header header = this.      Create

Instantiate the Sub-template
header Header = this. Create

The Copypropertiesto method compares the properties defined in the main template and the attributes in the child template, and then copies the property values from the primary template to the child template if it is found to be the same as the property name type defined in the template from the properties defined in the master template.

Set Child template Properties

To create an instance of a child template in the main template, you can go directly through the Create method [CSharp] view plain copy print? Instantiate the Sub-template header header = this.      Create

Instantiate the Sub-template
header Header = this. Create

The header in Create is a type defined by name when registering a child template, and after creating an instance of the child template, you can access the properties in the child template directly through the properties of the instance, such as one of the attributes defined in the Includemeta for the child template. Creating an instance of a child template from the output of the child template, setting the properties of the child template, and allowing the child template to output the results in the main template, there are several ways to export the content from the child template. The first one is the results of the handle template generated directly into the main template [CSharp] view plain copy print?   Instantiate the sub-template. Header Header = this.   Create

Instantiate the sub-template.
Header Header = this. Create

The second method is to output the results to a separate file:
[CSharp]View Plain copy print?   Instantiate the sub-template. Header Header = this.   Create
Instantiate the sub-template.
Header Header = this. Create

Specific examples can be found in the Codesmith tutorial (3): Automatic generation of Yii Framework ActiveRecord

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.