Codesmith batch generation of template files

Source: Internet
Author: User

In the past, when I wrote a codesmith template, If I directly mixed the code like ASP,CodeThe output is in codesmith. I don't know how to save it to the file. Later I concatenate all the template text to be output with stringbuiler and then I/O to the file. But it is too troublesome to write codesmith, ui highlight and smart prompts are not ideal, so they are very messy and cannot generate all class templates at once (it is too messy to write all the templates in one, which is inconvenient to manage, but after looking at the examples in codesimth, it is found that other people's templates are independently developed one by one, and a scheme is generated using a primary template, A single template is used to generate a single template, so it took some time to read the nettiers generation method and finally found the eye, here, I will send a reference to a friend who has not solved this problem!

Step 1:Next, register the subtemplate you want to generate to the master template. The Code is as follows:

<% @ Register name = "myentityclass" template = "C: \ myentityclass. CST" mergeproperties = "false" excludeproperties = "" %>

Name = "myentityclass" template seems to be registered as a codetemplate derived class, And myentityclass will be the name of this class;

C: \ myentityclass. CST is the location of your template. The relative location is also acceptable;

Step 2:Add attributes to a template

Of course, attributes need to be set in our independent sub-template. reflection can be used to assign values. The Code is as follows:

Codetemplate = new myentityclasskey ();

Codetemplate. setproperty ("includeget", includeget );
Codetemplate. setproperty ("includefind", includefind );
Codetemplate. setproperty ("includesave", includesave );

The previous includeget and includefind are some attributes of the template c: \ myentityclass. CST! The value set for it is later!

Step 3:Output file

Codetemplate. rendertofile ("C:/3.txt", true );

Done! The first parameter is the output address, and the second parameter is overwrite!

There are a lot of reloads, so I won't mention them! You can watch it slowly!

What is the purpose of this function? Of course it works. After writing a series of templates, it is not too tired to generate them one by one, and it is similar to setting parameters. With this function

I can generate them once, which is easy to understand!

Okay, so far, we have simply generated the nettiers template to steal it! If you are not familiar with codesmith, you can refer to the tutorials of other predecessors in the garden.

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.