Recommended: ASP Tutorial: Tips for ASP to generate pseudo-parameters Two days ago, I was working on an in-site version of the enterprise search engine and found that some sites can link to site content. . I looked at it strangely and found that it was linked according to the automatic numbering rules of the database ID~~ I made the following thing in my spare time, hoping it will be helpful to everyone in the future writing ASP programs! % ''// Generate random number of specified digits''// You can also use Guid
In order to avoid maintenance difficulties caused by mixing ASP programs and HTML code, this article introduces a method to use templates to separate programs and pages to make programming easier.
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. Not to mention that 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:
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:
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:
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:
%=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:
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
!#TPLDEFTHIRD Template. !#TPLENDTHIRD
!#TPLENDSUB
-
So:
tpl.Split(Main);
After execution, new template variables SUB and THIRD will be generated, and their content is the statement between !#TPLDEFSUB and !#TPLENDSUB.
Moreover, the content of Main template variables will also change:
The content of tpl.Main is: This is the main template. Next is {SUB}
The content of tpl.SUB is: SUB sub-template, and {THIRD}
The content of tpl.THIRD is: THIRD template.
The statement blocks defined by TPLDEF and TPLEND are filled with many re-nested.
4. Template processing: Template.Parse(Name)
Parameter: Name (string type) is a template variable.
Replace the string in the template with curly braces with the contents of the template variable of the same name.
Example: Continue with the previous example
%=tpl.Parse(Main)%
Show: This is the main template. Next is the SUB sub-template, and {THIRD}
As can be seen from the example, Parse only replaces the {SUB} variables in the Main template, and cannot be replaced in nested ways. This was designed deliberately to increase program flexibility. So how do you display the Main template in full?
example:
tpl.SUB=tpl.Parse(SUB);//First process the SUB variable, then process the Main variable.
Response.write(tpl.Parse(Main));
5. Customize template variables.
Customizing template variables is simple, you can directly use assignment statements to define and modify any variable:
example:
tpl.Hahaha=This is a custom variable;
tpl.THIRD=change the THIRD variable in the original template;
It should be noted that since JScript is case sensitive, you must pay attention to the spelling of upper and lower case. Generally speaking, template variables defined in HTML templates are in uppercase.
In addition, the TplPath, Load, Parse, and Split variables used in the template are used internally. Do not use them as well, otherwise the program may have an exception.
Here is a complete example:
Step 1: Create the Html template file first.
Here we first explain the composition of the HTML template file. First of all, it is almost no different from ordinary HTML files, except that there are a few more tags.
There are two types of markings for templates. Let's first look at an example:
TEST.HTM
-!File name: TEST.HTM
HTML
TITLE Example/TITLE
HEADER
/HEADER
BODY
This is a table example.
TABLE
!#TPLENDMAXX10!#TPLENDMAXX
!...Note that a trick is used here, which is to define the MAXX template variable and assign the value to 10.
TR
TDX/TDTDX squared/TD
/TR
!#TPLDEFROW
TR
TD{X}/TDTD{XX}/TD
/TR
!#TPLENDROW
/TABLE
There are {COUNT} rows of data above.
/BODY
/HTML
-
As can be seen from the above, notations like {X}, {XX}, {COUNT} are the definition template variables. They will be replaced in the ASP program.
And!#TPLDEFROW...!#TPLENDROW is to define a statement block ROW. You can repeat the ROW blocks multiple times in the ASP program.
Step 2: Design the ASP program.
TEST.ASP
-%@Language=JScript%
!#includefile=Template.JScript.INC
%
vartpl=newTemplate(c://Inetpub//wwwroot);
varstr=;
vari;
tpl.Load(Main,TEST.HTM);
tpl.Split(Main);
tpl.COUNT=0;
for(i=1;i=tpl.MAXX;i++)//tpl.MAXX is defined as 10 in the template.
{
tpl.X=i;
tpl.XX=i*i;
str+=tpl.Parse(ROW);
tpl.COUNT++;
}
tpl.ROW=str;
tpl.MAXX=;//Clear this template variable to avoid being displayed.
%
%=tpl.Parse(Main)%
-
The above program will display a square table of 1 to 10.
Usually when using templates, just add a statement showing the page to the last line. Therefore, the whole program appears very clear. At this time, just edit the template file to change the appearance of the entire page.
As for the template file, it can be any file, such as HTML files, ASP files, or even the program itself!, and multiple templates can be loaded in a program to work together, which not only provides great flexibility, but also minimizes the correlation between the template file and the ASP program.
Making good use of templates will make your work easier.
Attachment: Template source program
!File name: Template.JScript.INC
%
/******************************************************/
/*TemplateClass*/
/*Author:*/
/*Date:6-09*/
/******************************************************/
//TemplateMethodDefine
functionTemplate_Parse(name)
{
if(this[name]==null)
return;
varreg=newRegExp({(//w*)},ig);
varstr=newString(this[name]);
vararr=str.match(reg);
vari;
if(arr!=null)
for(i=0;iarr.length;i++)
{
key=arr.slice(1,-1);
reg=newRegExp(arr,ig);
if(this[key]!=null)
str=str.replace(reg,this[key]);
}
returnstr;
}
functionTemplate_Split(name)
{
varlen=0;
vararr;
if(this[name]==null)
return;
varTemplate_Exp=newRegExp(!#TPLDEF+(//w*)*((.|//n)*)!#TPLEND+//1*,i);
while(this[name].search(Template_Exp)!=-1)
{
arr=this[name].match(Template_Exp);
this[arr[1}=arr[2];
this[name]=this[name].replace(Template_Exp,{+arr[1]+});
this.Split(arr[1]);
}
}
functionTemplate_Load(name,filename)
{
varfso=newActiveXObject(Scripting.FileSystemObject);
varfile=fso.BuildPath(this.TplPath,filename);
if(fso.FileExists(file))
{
varf=fso.OpenTextFile(file,1);
this[name]=f.ReadAll();
}
}
//TemplateConstructor
functionTemplate(path)
{
//Property
this.TplPath=path;
//Method
this.Parse=Template_Parse;
this.Split=Template_Split;
this.Load=Template_Load;
}
%
Share: ASP Tutorial: A Simple AJAX Example ASP tutorial: A simple example about AJAX Index.asp: Program code html head meta http-equiv=Content-Type content=text/html; charset=gb2312 / titleAJAX simple application/title script language=javascript //Create XMLHttpRequest object var request = false; try { re