When using ASP to create a site, there is often a situation where the program code and HTML code are mixed in an ASP file. There are many disadvantages to doing this:
1. When programming, you must design and arrange the page layout, which will cause the code to be confused and difficult to understand and irregular;
2. When you need to change the appearance of the page, you not only need to change the HTML part, but also need to change the ASP code, which is not easy to maintain.
So, how can we avoid these troubles?
The answer is to use a template file to separate the ASP code and HTML pages, and all problems will be solved. Using templates has the following benefits:
1. The appearance of the entire site can be replaced in a very short time;
2. Enable programmers to abstract programming without having to touch HTML code;
3. The previous template can be reused.
Programs that have used PHP will know that PHP has a template program (FastTemplate). The current problem is how to implement similar functions in ASP.
Microsoft's ASP comes with two scripts: VBScript and JScript. They all come with a regular expression object (RegExp). Using string objects and RegExp objects, you can easily implement template functions. Mu Feng used this to write a Template.JScript.INC file, and the content of this file is attached to the end of the article. A competent reader can improve according to their needs.
Here is a description of how to use it. Since this file is written in JScript (of course it is easy to convert it to VBScript), the default scripting language should be set to JScript, that is, the first line of the ASP program should be: %@Language=JScript%, and then the template program file is included: !#includefile=Template.JScript.INC.
Let me first introduce the use of the Template class:
1. Create a Template object: Template(Path)
Parameters: Path (string type) HTML template file storage path.
Use the new operator to create a Template object.
example:
The following is a quoted snippet:
vartpl=newTemplate(c:/template);
In the program, you can use tpl.TplPath to get the template path, or you can use tpl.TplPath to change the template path.
like:
The following is a quoted snippet:
tpl.TplPath=d:/template;
2. Load the template file: Template.Load(Name,File)
Parameter: Name (string type) is a template variable name.
File (string type) template file name. This file is stored in the HTML template path.
Read the file File into the template variable Name.
example:
The following is a quoted snippet:
tpl.Load(Main,TEST.HTM);
At this time, the template variable Main contains the content of the file TEST.HTM.
You can use tpl.Main to access the template variable Main.
example:
The following is a quoted snippet:
%=tpl.Main%
The content of the TEST.HTM file you just read will be displayed.
3. Template Split: Template.Split(Name)
Parameter: Name (string type) is a template variable name.
Decompose the sub-template in Name.
example:
The following is a quoted snippet:
Let's first assume that the TEST.HTM content in the above example is:
-
This is the main template. Next is:!#TPLDEFSUBSUB sub-template, and