Syntax in the codesmith Template

Source: Internet
Author: User
Tags code tag xml attribute
This article describes the syntax in the codesmith template.
Tag
Tags are usually displayed in the template header and are used to set many different attributes.
Code template declaration (codetemplate Directive)
This is the only required declaration in the template. It contains some special attributes of the template, including the language used by the template, the language generated, and some descriptions of the template.
Example: <% @ codetemplate Language = "C #" targetlanguage = "C #" Description = "generates a class." %>

Parameter introduction:
Language: The language used for Template Development, such as C #, VB. NET, and JScript.
Targetlanguage: it is only a classification of template code and does not affect the generated code language. Is an attribute of the template, indicating that the template should generate the corresponding code based on that language. For example, you can use codesmith to generate C # code from any language.
Description: For some template description information, this information is displayed when you select this template in codesmith explorer.
Inherits: All codesmith templates are inherited from codesmith by default. engine. codetemplate, which provides some basic functions used by the template, such as ASP.. NET page class. The attributes of these inherited classes can be modified, but these new classes must inherit from codesmith. engine. codetemplate. Codesmith can also find this class. Of course, you need to introduce a component to include this class.
SRC: in some respects, SRC is similar to inheriting inherits. They allow you to include some functions into the template from other classes. The difference between these two attributes is that SRC allows classes to be dynamically compiled with your template, While inherits only allows you to provide a compiled class or component.
Debug: You can determine whether the template can contain debugging symbols. If you set this attribute to true, you can use the system. Diagnostics. Debugger. Break () method to set the breakpoint.
Linepragmas: if it is set to true, the template error will be directed to the source code of the template. If this parameter is set to false, the template error is redirected to the compiled source code.

Property Directive)
The attribute is used to declare a used parameter during template running, for example: <% @ property name = "classname" type = "string" default = "class1" Category = "context" Description = "the name of the class to generate" Optional = "true" %>

Attribute parameters:

Name: name of the parameter used by the template.
Type: The parameter type can be any. Net valid data type, such as a simple string type or codesmith schemaexplorer. databaseschema type. Note that the type must be the type of the base class library, for example, replacing string and INT with string or int32.
Default: Set the default value.
Category: specifies the type of the Property Displayed in the attribute panel of codesmith explorer, such as drop-down selection and direct input.
Description: description of this attribute on the property panel.
Optional: Set whether this attribute is required. If it is set to true, this parameter value is optional. If it is set to false, this parameter must have a value.
Editor: Specifies the GUI editor used when you enter the property value in the property panel.
Editorbase: the basic type used by the editor. If not described, uitypeeditor is the default editor.
Serializer: I am not sure about this part. :) The serializer parameter specifies the ipropertyserializer type to use when serializing the properties values. This is equivalent to using a [propertyserializerattribute].

Xmlproperty Directive)
Example:
Code tag
<%>Tag
You can place any number of codes in it, but it cannot be directly output to the template.

<% Foreach (columnschema column in sourcetable. columns) {%>
<% = Column. name %>
<% }%>

<% = %>Tag

Output a string in the template. <% = Column. name %>

Script tag

This label can contain a piece of code, but it does not directly affect the output template. You can place some helpful methods in it, and then you can call it in various places of the template. This parameter runat = "template" must be included in the script tag; otherwise, it will be processed as normal text.

Example:

1 <SCRIPT runat = "template">
2 Private string getcolumnname (columnschema CS)
3 {
4 return CS. Name;
5}
6 </SCRIPT>
7
8 <% foreach (columnschema Cs IN sourcetable. columns) {%>
9 <% = getcolumnname (CS) %>
10 <% }%>

 

Using tags can greatly reduce code and make templates easier to read and manage.

 

IncludeTag

Like ASP. NET, a template can contain some text files, but like ASP. NET, it does not always achieve your goal.

Example:

<! -- # Include file = "myfile. Inc" -->

 

Sometimes, you can reference the functions of one component in multiple templates and call the methods. In this case, we reference the component. However, in some cases, applying the include label can achieve better results.

CommentTag

Annotation label, which has been introduced in the front.

Example:

<% -- This is a comment -- %> <% @ xmlproperty name = "entitymap" schema = "entitymap. XSD "Optional =" false "Category =" context "Description =" entitymap XML file to base the output on. "%>

XML Attribute parameters:
Name: Name.
Schema: this parameter is used to specify an XSD file and create a strong object model. If this plan is specified, the compiler will try to analyze the XSD file and generate a strongly typed object model for this plan, so that strong types and intelligence can work collaboratively with XML in the template. If this plan is not set, this parameter is of the xmldocument type and will use the xml dom to navigate to an XML content and generate code.
Category: the category in the codesmith attribute panel.
Description: description.
Optional: whether the parameter is required. If it is set to true, the parameter is not required, and false is required. When it is set to false, codesmith cannot continue running if the user does not provide a parameter.

Registration Statement (register directive)
This attribute is usually used to introduce another template file and be compiled together with the current template file. This is an interactive method that uses subtemplates.
Example:

<% @ Register name = "mysubtemplate" template = "mysubtemplate. CST" mergeproperties = "true" excludeproperties = "someexcludedpropertyname, someproperties *" %>

Once a template is registered, you can create an instance of the template and set its attributes as follows:

1 <SCRIPT runat = "template">
2 Public void outputsubtemplate ()
3 {
4 mysubtemplate = new mysubtemplate ();
5
6 // set an individual properties value.
7 mysubtemplate. someexcludedpropertyname = "somevalue ";
8
9 // copy all properties with matching name and type to the sub template instance.
10 This. copypropertiesto (mysubtemplate );
11
12 // render the template to the current templates response object.
13 mysubtemplate. Render (this. Response );
14
15 // render the template to a file.
16 mysubtemplate. rendertofile ("C:/somefile.txt ");
17}
18 </SCRIPT>

Registered parameters:
Name: name of the introduced template. It can be used as an instance to create a template.
Template: The relative path of the introduced template file. It can be dynamically compiled with the current template.
Mergeproperties: when it is set to true, the attributes of all referenced panels are dynamically added to the current template.
Excludeporperties: when using mergeproperties, you may not need to add certain attributes to the current template. You can separate unnecessary attributes with commas (,). The asterisk (*) can be used as a wildcard.

Assembly directive)
Used to reference an external component in a template or contain a compiled source file.
Example:

<% @ Assembly name = "schemaexplorer" %>

Or

<% @ Assembly src = "mysourcefile. cs" %>

Codesmith automatically loads some different components: system, system. diagnostics, system. componentmodel, Microsoft. VisualBasic, codesmith. Engine

Component parameters:
Name: name of the component to be referenced. The component must exist in Global Assembly Cache and be in the same path as codesmith or in the same path as the template file.
SRC: The relative path of the file to be included.

Import direve ve)
Introduce a namespace in the template, which is the same as imports in VB. NET and using in C.
Example:

<% @ Import namespace = "schemaexplorer" %>

Introduced parameters:
Namespace: name of the namespace to be introduced. Remember to load the corresponding component that contains the namespace at the same time, unless the component is loaded by default.

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.