Update template class and some experience

Source: Internet
Author: User
Tags php template server memory

Download/files/vitality2007/templateweb.rar

No matter what server technology is used. The client accepts HTML strings. In the accepted projects, I used an extreme method. Completely abandon the server-side controls. Write a function, a table, and a TD to spell out the final display page. It is a bit similar to the previous ASP development method. Do not use server-side controls. It can speed up page generation and save server resources. Even Microsoft advocates avoiding using server-side controls as much as possible. Ajax is used to implement server-side control delivery for a better user experience.

To facilitate development, I borrowed some ideas about the PHP template class. Developed a template class that is fully suited to Asp.net. The template class is used to help you complete the final HTML String concatenation of the page. Programmers only need to complete the key logic. Data processing. Tedious HTML String concatenation and synthesis are all completed by the template class. A tag is not just a combination of fields. Use the delegate in. net. It is convenient to combine with a function call. This increases the flexibility of tag output results. This completely avoids the performance loss caused by reflection. I personally think that the delegate should be used to develop the template class of Asp.net.

Directly modify the parameters of tags in the template and control commands. It can be recompiled without re-compiling. Dynamic Control page program output after compilation. The result is that when the website modifies the page, many of the original results must be re-modified and re-compiled. Now, maintenance personnel only need to modify the tag parameters. Of course, to add a function, you must add a new tag. Use W3C ideas. The style class name is used as the parameter value of the tag, and the style is directly uploaded to the tag parsing function to generate the parsing result. You can only change the tag parameters and use different styles. So that the same page has two completely different forms. The revision is too convenient.

After obtaining the static page of the artist. Generally, the template class is used to cut out the parts that can be separated from the page. Form a specialized Page Template fragment. The programmer replaces the part of the data logic in the template fragment with a tag. Write the corresponding tag parsing function. After the template class resolves fragments, the parsed results can be written to the shtml file to include dynamic or static pages. You can also output the resolution result directly on the dynamic page. Of course, you can also directly write it to a static page. Update static pages. You can modify the template fragments and display the page by yourself.

In my example, the read template generates a new static page or directly displays the page. However, the algorithm in the template class indicates that the larger role is to read the template fragments. And the parsed string is directly displayed on the dynamic page.

I am relatively lazy and have no time. In addition to optimizing the performance of the template class. Examples of using methods have hardly been updated. You can see that the site www.93913.com at this address is completely spelled out using a template class, a table, and a table, a vast majority of pages. All have corresponding template pages. The blog part is fully implemented by the template. Proficient in template class, and developing such projects. Development speed is no slower than using controls. This website is developed using a regular template class. In addition to labels. The command block and loop are not supported. Templates and some analysis data are not cached. In my personal testing, the positioning speed of regular template tags is more than 9 times slower than that of pointer template tags. This site indicates that it is feasible to use Template Development to completely spell out the site output on the page.

Introduction:
This template class is written for the development of a three-tier architecture large web site. The stringtempletreplet. dll component contains a template class.
The template class in the stringtempletreplet namespace can be used at the UI Layer or the logic layer.

**************************************** **************************************** ****************

Running method:
Create a virtual directory named templateweb that is similar to the templateweb folder.
Run the internal default. aspx page. The corresponding description is provided on the CS file on the page.
When you open the page for the first time, you should keep the delegate object and template file, and the tag will parse the data to the cache. Therefore, the real speed depends on what you do after the first time. Of course, the current development trend is to try to use the data in the memory, should be the current server memory is generally large. The bottle diameter usually occurs in connection with the database, and is rarely used in memory. Of course, if your server memory is small, you can try to use a small template page.

**************************************** **************************************** ****************

Principles:
A tag replacement template. The pointer operation in C # is used to find the tag location.
The delegate object refers to the parsing function corresponding to the tag, and uses the static object to cache all the delegate objects.
Cache all template content without changing the template body. The cache template parses data. You can change the template dynamically by changing the configuration.
You can also change the template dynamically. It is faster.

**************************************** **************************************** ****************

Label introduction:
The content between {$ and $} is a tag.
For example
{$ Tag Name: parameter 1: parameter 2: parameter 3: character 10 $}
{$ Tag Name: parameter 1: parameter 2: byte 10 $}

In a tag, use the following symbol to divide the tag into two parts: Tag parameter and Command Control.
{$ Tag Name: parameter 1: parameter 2: parameter 3: byte 10 $}
Tag parameters: Command Control

Separate the tag parameter part and the command control part with the following two parts:

Tag parameter section: the first is the tag name, which is the corresponding name of the tag parsing function. Later, it is a function parameter. The tag parsing function accepts all parameter types as string type, the number of parameters is 0 to 20, and the return type is string type.

Command Control: Currently, there are only two command symbol characters and bytes, followed by a numeric parameter, indicating the length of the tag parsing result. I will expand other command control symbols in the future

For details about the tags and functions, see the description in the business. CS file.

For specific tag usage rules, see the description in the templet/aa1.htm template.

For specific call rules, see the three aspx pages in the project.

Writearticon. aspx: A news system that reads template parsing and writes files to generate static pages.

Returntext. aspx: Read the template to parse the returned string, use and write the dynamic page that often changes the appearance, and change the page surface of the skin.

**************************************** **************************************** ****************
 
Dynamic tag values:
In addition to the fixed parameter values sent from the tag, the tag parsing function. Dynamic parameters can be accepted in three ways.
After the able and hashtable objects are passed as the template class
(Datatable is obtained from the data layer. hashtable is generally a set of variables in the page and other variables to be passed recently)

1. DT [5] [name]: the value in the name column of the five rows in the able. The first column is a number, and the second column is a column name.

2. DT [name]: the value in the name column of the zero row in the able is taken out of the loop. One parameter indicates the column name.

In a loop, the value of the name column in the able is taken, and the rows are determined by the number of parameters of the loop label.

3. Page [pageid]: obtains the value on the pageid key of the hashtable object, and returns the string type.

For example, {$ two parameter bookmarks: dt [3] [word]: page [userid]: character 10 $}

{$ Dt [5] [name] $} {$ dt [name] $} {$ page [pageid] $} or
{$ Dt [5] [name]: character 10 $} {$ dt [name]: character 10 $} {$ page [pageid]: character 10 $}
This label indicates that the value in the object is displayed directly on the page.

**************************************** **************************************** ****************

Call the template class method on the page or in the class:

Can be called on pages or within a class
 
Generate an object and associate the corresponding tag function parsing class
// The tag parsing function in the templet/aa1.htm template is in the business. CS class.
// You only need to make the business. CS class inherit the functionlist class.
// Here, we only need to add a business object after this line in new business ().
// Use the interface to implement polymorphism, and the code looks a little troublesome. You only need to modify the last "New Business ()" to the class object containing the tag parsing function of the user.
Ilabelanalystart objilabel = (ilabelanalystart) New templetreplet (New Business ());

// Read template path attributes
Objilabel. readfilepath = This. server. mappath ("templet/aa1.htm ");

// Obtain the data table object
Objilabel. labeldatatable = Ds. Tables [0];

// Use the hashtable object to pass the variables on this page to the near template class.
Objilabel. labelhashtable = HT;

// Return the resolution result
String AA = objilabel. laberreplettext ();
 
For details, refer to the CS files of the three files.
Writearticon. aspx -- read template parsing Write File used to generate static page news system

Returntext. aspx -- read the template to parse the returned string and write the page that often needs to change the appearance and skin.

**************************************** **************************************** ****************

Tag function Association:
For details, see the business. CS file.

Add the following code to the static constructor of the class corresponding to the logic layer.
 
// Call the static constructor to delegate the TAG body and execution function to the image.
// The static constructor must be executed.
Static business ()
{
Business _ this = new business ();
// Generate an object of this class temporarily in order to use _ this. To point out all functions

Business. addlabel ("No parameter bookmarks", new run_0 (_ this. Show ));
Business. addlabel ("a parameter bookmarks", new run_1 (_ this. Tag ));
Business. addlabel ("Two Parameter bookmarks", new run_2 (_ this. tag_two ));

// Associate bookmarks with execution functions. Put it in the static constructor of the bookmarks parsing class.
// It is to make full use of objects in the memory, not every time
// The Public String tag (string aa) function corresponding to the "one-parameter bookmarks" Character

// ("A parameter bookmarks", new run_1 (_ this. Tag )));
// Tag Name |
// Use run_1 to delegate a parameter. |
// Corresponding tag execution function

// There are 0 or more examples above
}
 
For details, see the business. CS file.

**************************************** **************************************** ****************

Loop in a tag:
Some special symbols can be used in a tag to implement non-nested loop.

//------------------------------------------------------------------------
{$ Loop (3,0, Alter) $} // three rows are displayed, starting from row 0. If there are alternate columns, run the alternate columns.

{$ Blockitem $} // The default cyclic row,

<TD bgcolor = "#33 CCFF"> title: {$ dt [5] [name] $}, Author: {$ a parameter: DT [dddf] $} </TD>

{$/Blockitem $}

{$ Blockalteritem $} // There are no default row tag pairs, and there are alternate row tag pairs. Use the alternating tag pair as the default // row tag pair

<TD bgcolor = "# ff6699"> title: {$ dt [5] [name] $}, Author: {$ a parameter: dt [dddf] $} </TD>

{$/Blockalteritem $}

{$ Blockpatch $} // when the number of rows to be cyclic is greater than the number of rows in the datatable, use the content here to add the display. If no signature is added. When the number of rows displayed is greater than the number of rows in the datatable //, only the number of rows in the datatable is displayed.

<TD bgcolor = "#33 CCFF"> supplement (the tag can also be used inside) </TD>

{$/Blockpatch $}

{$ Blockalterpatch $} // No tag pair is added, and there are alternate tag pairs. Add the alternative tag pair when adding/charge the tag pair

<TD bgcolor = "# ff6699"> alternate addition (tags can also be used inside) </TD>

{$/Blockalterpatch $}

{$/Loop $} // The End of the loop
//--------------------------------------------------------------
 
{$ Blockitem $ }{$/blockitem $}: the default cyclic row label pair

{$ Blockalteritem $ }{$/blockalteritem $}: alternating loop row label pair

{$ Blockpatch $} {$/blockpatch $}: adds a row tag pair by default.

{$ Blockalterpatch $} {$/blockalterpatch $}

//------------------------------------------------------
Only the default loop row can be added without the {$ blockitem $} tag, as shown in
{$ Loop (3,0, Alter) $}
<Tr> <TD >{$ function tag $} </TD> </tr>
{$/Loop $}
And
{$ Loop (3,0, Alter) $}
{$ Blockitem $} <tr> <TD >{$ function tag $} </TD> </TR >{$/blockitem $}
{$/Loop $}
Yes indicates the same effect.
//-------------------------------------------------------------
 
 
**************************************** **************************************** ****************

Loop parameter description:
{$ Loop (3,0, Alter) $}
There are three parameters in the loop, which can be appropriately reduced.

The first parameter:
3: loop 3 times. None indicates all loops.

The second parameter:
2: Starting from 2. No value indicates starting from 0.

Third parameter:
Alter: Alternate
Noalter

If all three parameters are not written, at least one pair of empty parentheses should be retained {$ loop () $}
Indicates the number of rows in the cyclic able. The number starts from 0 and is supplemented.

**************************************** **************************************** ****************

It is recommended that the template not be larger than 85 KB and be careful to become a large object. That's the second generation object.
I am very happy to see the growth of the template class along with my own understanding.

 

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.