Texttemplate, a class used to dynamically generate static pages

Source: Internet
Author: User
Tags connectionstrings

Dynamic generation of static pages has many advantages. For example, generating HTML pages is helpful for indexing by search engines. At the same time, data access is reduced, database access is reduced, and web pages are opened more quickly. So I wrote a class for dynamically generating static pages -- texttemplate (the name seems to have nothing to do with static pages, because it also has other features that will be mentioned below)

The pages in the. NET document generation tool released by the author and the project files (HHP) and content files (HHC) required for CHM compilation are all generated using this class.

Basic Ideas:

Use a string as the page template, and then the page contains several signs (represented by {flag name}). When the page is generated, replace the flag with the corresponding value.

Implementation Method:

When the texttemplate instance is initialized, the template is read. The template is split into several parts by the mark. When a page is generated, you only need to simply connect the template content with the flag value. For example:

Suppose there is a template ABCD {tag1} EFG {tag2} hij {tag3} kmun

During initialization, the template is divided into four strings: "ABCD", "EFG", "hij", and "kmun,

Suppose tag1 = "123", tag2 = "456", tag3 = "789"

The generation is equivalent to executing "ABCD" + "123" + "EFG" + "456" + "hij" + "789" + "kmun"

Code: 

Download complete code

Using System; Using System. Collections. Generic; Using System. text; Using System. Text. regularexpressions; Using System. collections; Using System. IO; /// <Summary> ///  Indicates a text template, which uses a string as the template,  ///  Replace the flag in the template with the corresponding value (the flag in the template is represented by {flag name}) to generate a new text  /// </Summary>  Public class  Texttemplate {Texttemplatetag [] _ tags; String [] _ Contentparts; Int _ Tagcount; Private Texttemplate () {_ tagcount = 0; _ tags = Null ; _ Contentparts = Null ;} /// <Summary> ///  Initialize texttemplate with the specified Template  /// </Summary> /// <Param name = "content">  Template content  </Param>  Public Texttemplate (String Content ); /// <Summary> ///  Initialize texttemplate with the specified template, and read the template content into the file again  /// </Summary> /// <Param name = "file">  Template File Location  </Param> /// <Param name = "encoding">  Encoding used by the file  </Param>  Public Texttemplate ( String File, Encoding Encoding ); /// <Summary> ///  Read the template and split the template with the mark as the split point /// </Summary> /// <Param name = "content"> </param>  Private void Fromstring ( String Content ); /// <Summary> ///  Generate text with the specified value  /// </Summary> /// <Param name = "values">  Value corresponding to each flag (using the flag name as the key)  </Param> /// <returns>  Generated text  </Returns>  Public String Render ( Hashtable Values );/// <Summary> ///  Generate text with the specified value  /// </Summary> /// <Param name = "ARGs">  Value corresponding to each flag (ignore the flag name, the first flag corresponds to the first parameter, and so on)  </Param> /// <returns>  Generated text  </Returns>  Public String Render ( Params object [] ARGs ); /// <Summary> ///  Generate text with the specified value and save it to the file  /// </Summary> /// <Param name = "file">  Path of the file to be saved </Param> /// <Param name = "encoding">  File Encoding  </Param> // <Param name = "values">  Value corresponding to each flag (using the flag name as the key)  </Param>  Public void Saveas ( String File, Encoding Encoding, Hashtable Values ); /// <Summary> ///  Generate text with the specified value and save it to the file  /// </Summary> /// <Param name = "file"> Path of the file to be saved  </Param> /// <Param name = "encoding">  File Encoding  </Param> /// <Param name = "ARGs">  Value corresponding to each flag (ignore the flag name, the first flag corresponds to the first parameter, and so on)  </Param>  Public void Saveas ( String File, Encoding Encoding, Params object [] ARGs ); /// <Summary> ///  Separate a template with a specified separator into a small Template  /// </Summary> /// Suppose there is a template ABCD {tag1} EFG {tag2} hij {tag3} kmun  ///  Call split ("tag2") to generate two templates:  ///  ABCD {tag1} EFG  ///  Hij {tag3} kmun  /// <Param name = "splittag"> </param> /// <returns> </returns>  Public Texttemplate [] Split ( String Splittag );} Internal class  Texttemplatetag { Int _ Position, _ length;String _ Name; Public Texttemplatetag ( String Name, Int POs, Int Len) {_ name = Name; _ Position = Pos; _ length = Len ;} Public String Name { Get { Return _ Name ;}} Public int Position { Get { Return _ Position ;}}Public int Length { Get { Return _ Length ;}}}

Instance code: 

Static classProgram{[Stathread]Static voidMain () {texttemplate temp =NewTexttemplate ("); Console. writeline (temp. Render ("Pic.bmp","Image"); Hashtable values =NewHashtable (); values. Add ("Src","Pic.bmp"); Values. Add ("Alt","Image"); Console. writeline (temp. Render (values ));}}

Output:

 Other applications:

Texttemplate can also be used to generate a web. config file when installing a website. You only need to define the following template:

 <?  XML  Version  = " 1.0 " ?> <  Configuration  Xmlns  = " Http://schemas.microsoft.com/.NetConfiguration/v2.0 " > <  Appsettings  > </ Appsettings  > <  Connectionstrings  > <  Add  Name  = " Djdb. localsqlserver " Connectionstring  = " {Connectionstring} " Providername  = " System. Data. sqlclient "/> </  Connectionstrings  > Other configurations </  Configuration  > 

You can set the connectionstring value. This method is more convenient than the xmldocument class.

Summary:

Texttemplate has the following advantages:

1. The template is analyzed and separated for storage only during initialization. When multiple pages are generated using the same template, the content of the template is simply connected with the value of the flag, you do not need to analyze the template every time. If you use the string replace method, you need to analyze the string every time. If the flag value contains a flag, the generated page will be affected.

2. The template can be read from the file, so the template file can be edited using various webpage creation tools.

Texttemplate is simple, but has many functions...

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.