I have been doing web development for several years. I have different understandings in different companies. I recently worked on a blog project. I realized the role of template classes in web development. I am at home at night. I always thought that I could make a template class specially designed for asp.ne development. So I wrote this template class.
Download address click here
Just rename the downloaded 500sea.txt file to Templet.rar and open it.
if open
http://img.club.163.com/images/user/other/500sea
There is no prompt to download the 500sea.txt file. Then use Internet Express and other tools to download this address and change the downloaded 500sea.txt file into a *.rar file.
Below is a description file inside
//------------------------------------------------ -------------------------------------------------- --------------------------//
introduce:
This template class is written for three-tier architecture web site development. StringTempletReplet.dll component
Contains template classes. The template classes in the StringTempletReplet namespace can be used in the UI layer or
to be used in the logic layer.
*************************************************** ***************************
How to run:
Create a virtual directory named Templet to refer to the decompressed Templet folder.
Run internal RetuenString.aspx, ReturnText.aspx, WriteArticon.aspx,
LongLoop.aspx page. There are corresponding instructions on the cs file corresponding to the page.
When the page is opened for the first time, the delegate object and template file should be saved in the cache, so the real speed depends on the first page.
One time later.
*************************************************** ***************************
Principle introduction:
A template for label replacement. Used pointer arithmetic in c# to find the label location.
Use the delegate object to point to the parsing function corresponding to the image tag, and use static objects to cache all delegate objects.
Cache the entire template content without changing the template body. Templates can be changed dynamically.
*************************************************** ***************************
Tag introduction:
The content between the pair of special symbols {$ and $} is a label
like
{$Tag name:Parameter 1:Parameter 2:Parameter 3::Character 10$}
{$tagname:parameter1:parameter2::byte10$}
{$dt[5][name]$}
The label is divided into two parts (label parameter part) and (command control part) through the :: symbol inside the label.
{$tagname:parameter1:parameter2:parameter3::byte10$}
(Tag parameter part) :: (Command control part)
In these two parts, separate the label parameter part and the command control part with a : sign.
Tag parameter part---the first one is the tag name, which is the corresponding name of the tag parsing function, and the subsequent ones are the parameters of the function.
The tag parsing function accepts all parameter types as string type, and the number of parameters is 0-20.
Function, the return type is string type. Parameters can be hard-coded on the label or dynamically written.
The command control part---now there are only two command symbol characters and bytes, followed by a numeric parameter, indicating the interception standard
The length of the signature parsing result, such as 10 characters and 10 bytes.
I will expand other command and control symbols in the future.
For the correspondence between labels and functions, please see the instructions in the Business.cs file.
For specific usage rules of tags, please see the instructions in the template/templet/aa1.htm.
For specific calling rules, please see the three aspx pages in the project.
WriteArticon.aspx - News system that reads templates and parses written files to generate static pages
ReturnText.aspx - Read template parsing and return strings, used for writing pages that often need to change their appearance.
Skinned page. Templates can be changed dynamically.
RetuenString.aspx - accepts tag strings and returns parsing results. Commonly used with ajax.
*************************************************** ******************************
Label dynamic value:
The tag parsing function can accept fixed parameters passed in the tag. There are three ways to accept dynamic parameters
After passing Datatable and Hashtable objects to the template class
(Datatable is obtained from the data layer, Hashtable is generally page variables and other variables)
1. dt[5][name] ---- means to get the value in the name column of 5 rows in the Datatable, the first one is a number,
The second is the column name
2. dt[name] ---- Outside the loop, it means taking the value in the name column of row 0 in the Datatable.
One parameter represents the column name
---- In the loop, it means to take the value on the name column in the Datatable, take that
These lines are determined by the parameters of the loop tag.
3. page[PageId] ----- Represents taking the value on the PageId key in the Hashtable object and returning 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$}
Such a tag indicates that the value in the object is displayed directly on the page.
*************************************************** ***************************
Call the template class method within the page or class:
Can be called within a page or class
Generate objects and associate corresponding tag function parsing classes
//Here we just need to add a Business class object after this line new Business()
//Using interfaces to implement polymorphism, the code looks a bit cumbersome. When you use it yourself, just modify the last "new Business()" to your own class containing the tag parsing function.
ILabelAnalyStart objILabel=(ILabelAnalyStart) new TempletReplet(new Business());
//Read template path attribute
objILabel.ReadFilePath=this.Server.MapPath("templet/aa1.htm");
//Write the file path attribute
objILabel.WritFilePath=this.Server.MapPath("page/aa1.html");
//Get the data table object
objILabel.LabelDatatable=ds.Tables[0];
//Pass the variables on this page to the template class using Hashtable objects
objILabel.LabelHashtable=ht;
Then three replacements can be achieved
//Start replacing and writing the template
objILabel.LaberReplet();
//return parsing result
string aa=objILabel.LaberRepletText();
//Return the parsing result. This directly accepts the template string. See the RetuenString.cs file for details.
string aa=objILabel.LaberRepletString();
For details, please see the cs files of these three files
WriteArticon.aspx - News system that reads templates and parses written files to generate static pages
ReturnText.aspx - Read template parsing to return a string and use it to write pages that often change their appearance.
Skin changing page
RetuenString.aspx - accepts tag strings and returns parsed results. Commonly used with ajax
************************************************* *******************************
Label function association:
Please see the Business.cs file for details.
In the static constructor of the class corresponding to the logic layer, add the following code.
//Call the static constructor and place the tag execution function delegate into the static hash table
//This method must be executed
static Business()
{
Business _this=new Business();
FunctionList.objFunctionHashtable.Add("No parameter bookmark",new KeyItemDelegate(new UserDelegate.Run_0(_this.show),0));
FunctionList.objFunctionHashtable.Add("One parameter bookmark",new KeyItemDelegate(new UserDelegate.Run_1(_this.tag),1));
FunctionList.objFunctionHashtable.Add("Two parameter bookmarks",new KeyItemDelegate(new UserDelegate.Run_2(_this.tag_two),2));
//Bookmarks are associated with execution functions. Put it in the static constructor of the bookmark parsing class. This is to fully use the objects in memory without executing them every time.
// "A parameter bookmark" character corresponds to the public string tag(string aa) function. The corresponding mode is to add key and value to the hash table.
// ("A parameter bookmark",new KeyItemDelegate(new UserDelegate.Run_1(_this.tag),1));
// tag name | | |
// Use the Run_1 delegate for one parameter. | |
// Corresponding tag execution function |
// For one parameter, write the number one
// 0 or more corresponding examples above
}
Please see the Business.cs file for details
****************************************** ***********************************
Introduction to loop within tags:
Non-nested loops can be implemented by using some special symbols within the tag.
//------------------------------------------------ --------------------------
{$loop(3,0,alter)$} //Display three lines, starting from line 0. If there are alternating columns, run the alternating columns.
{$BlockItem$} //Default loop line,
<td bgcolor="#33ccff">Title:{$dt[5][name]$}, Author:{$One parameter:dt[dddf]$}</td>
{$/BlockItem$}
{$BlockAlterItem$} // Alternately loop rows When there is no default row label pair, there is an alternate loop label pair. Treat the alternating label pair as the default row label pair
<td bgcolor="#ff6699">Title:{$dt[5][name]$}, author:{$one parameter:dt[dddf]$}</td>
{$/BlockAlterItem$}
{$BlockPatch$} //When the number of rows to be looped is greater than the number of rows in the Datatable, use the content here to supplement the display. If there is no "supplementary" tag pair. Then when the number of displayed rows is greater than the number of rows in the Datatable, only the number of rows in the Datatable will be displayed.
<td bgcolor="#33ccff">Supplement (tags can also be used inside)</td>
{$/BlockPatch$}
{$BlockAlterPatch$} //There is no supplementary label pair, but there are alternate supplementary label pairs. treat alternating supplementary tag pairs as supplementary tag pairs
<td bgcolor="#ff6699">Alternate supplement (tags can also be used inside)</td>
{$/BlockAlterPatch$}
{$/loop$} //end of loop
//------------------------------------------------ ---------------
{$BlockItem$} {$/BlockItem$}: Default looping row label pair
{$BlockAlterItem$} {$/BlockAlterItem$} : Alternately cycle row label pairs
{$BlockPatch$} {$/BlockPatch$}: Default supplementary line label pair
{$BlockAlterPatch$} {$/BlockAlterPatch$}: Alternately supplement row label pairs
//--------------------------------- -----------------------
If you only have the default loop line, you can not add the {$BlockItem$} tag pair.
like
{$loop(3,0,alter)$}
<tr><td>{$function tag$}</td></tr>
{$/loop$}
and
{$loop(3,0,alter)$}
{$BlockItem$}<tr><td>{$function label$}</td></tr>{$/BlockItem$}
{$/loop$}
It means the same effect
//------------------------------------------------ -------------
{$loop(3,0,alter)$}
{$BlockAlterPatch$}
<tr><td>{$function tag$}</td></tr>
{$/BlockAlterPatch$}
{$/loop$}
If there are only alternating cycle row label pairs and there is no default cycle row label pair then the alternating cycle row label pair will automatically be converted to
Default loop line label pairs
//------------------------------------------------ ---------------
{$loop(3,0,alter)$}
{$BlockAlterPatch$}
<tr><td>{$function tag$}</td></tr>
{$/BlockAlterPatch$}
{$/loop$}
If there are only alternating supplementary line label pairs and there is no default supplementary line label pair then the alternating supplementary line label pairs will automatically be converted to
Default loop line label pairs
*************************************************** **********************
loop parameter description:
{$loop(3,0,alter)$}
There are three parameters in loop, which can be reduced appropriately.
First parameter:
3: Loop 3 times, no means loop all
Second parameter:
2: Start from 2, does not mean start from 0
The third parameter:
alter: alternate
noalter: no alternation. If there is no indication, the default is alternation, so it is the same whether alter is added or not.
If all three parameters are omitted, at least a pair of empty brackets {$loop()$} must be retained.
Represents the number of rows in the loop Datatable
Starting from line 0, if there is a supplement, the supplement will be displayed.
*************************************************** *************************
There has been an update. Mainly optimization loop.
Now it takes 300-400 milliseconds to loop through a table with 50,000 rows.
*************************************************** *************************
It is recommended that the template should not be larger than 85k, and be careful of becoming a large-size object. It is recommended that the number of page templates for second-generation objects should be less than or equal to 919, and the number of corresponding function tags should not exceed 1597.
It's nice to see the template class grow along with my own understanding.
*************************************************** ***************************