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 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, the page layout must be designed and arranged, 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 "regex object" (RegExp), which can easily implement template functions using string objects and RegExp objects. Mu Feng used this to write a "" file, and the content of this file is attached to the end of the article. A competent reader can improve according to their needs.
The following 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: <!--#include file=""-->.
Let me first introduce the use of the Template class:
1. Create a Template object: Template(Path)
Parameters: Path (string type) The storage path of HTML template file.
Use the new operator to create a Template object.
example:
var tpl = new Template("c:\\template");
It can be used to obtain the template path in the program, or it can be changed by changing the template path.
like:
= "d:\\template";
2. Loading template file: (Name, File)
Parameters: 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:
("Main", "");
At this time, the template variable Main contains the contents of the file.
You can use it to access the template variable "Main".
example:
<%=%>
The content of the file you just read will be displayed.
3. Template Split: (Name)
Parameters: Name (string type) is a template variable name.
Decompose the sub-template in Name.
example:
First assume that the content in the above example is:
-------------------
This is the main template. Next is: <!--#TPLDEF SUB-->SUB sub-template, and
<!--#TPLDEF THIRD-->THIRD Template. <!--#TPLEND THIRD-->
<!--#TPLEND SUB-->
-------------------
So:
("Main");
After execution, a new template variable "SUB" and "THIRD" will be generated, and their contents are statements between <!--#TPLDEF SUB--> and <!--#TPLEND SUB-->.
And the content of the "Main" template variable will also change:
The content of the content is: "This is the main template. Next is {SUB}"
The content of the content is: "SUB sub-template, and {THIRD}"
The content of the content is: "THIRD template."
The statement blocks defined by TPLDEF and TPLEND are filled with many re-nested.
4. Template processing: (Name)
Parameters: Name (string type) is a template variable.
Replace the string enclosed in the template with curly braces with the content of the template variable of the same name.
Example: Continue with the previous example
<%=("Main")%>
Show: "This is the main template. Next is the SUB sub-template, and {THIRD}"
From the example, we can see that Parse only replaces the {SUB} variable 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:
= ("SUB"); //First process the SUB variable, then process the Main variable.
(("Main"));
5. Customize template variables.
Customizing template variables is very simple, and you can directly use assignment statements to define and modify any variable:
example:
= "This is a custom variable";
= "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 an exception may occur in the program.
Here is a complete example:
Step 1: Create the Html template file first.
Here we will 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:
-----------------
<!--File name:-->
<HTML>
<TITLE>Example</TITLE>
<HEADER>
</HEADER>
<BODY>
This is a table example.
<TABLE>
<!--#TPLDEF MAXX-->10<!--#TPLEND MAXX-->
<!--...Note that a trick is used here, which is to define the MAXX template variable and assign the value to 10. -->
<TR>
<TD>X</TD><TD>X Square</TD>
</TR>
<!--#TPLDEF ROW-->
<TR>
<TD>{X}</TD><TD>{XX}</TD>
</TR>
<!--#TPLEND ROW-->
</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 <!--#TPLDEF ROW-->...<!--#TPLEND ROW--> is to define a statement block "ROW". You can repeat the "ROW" block multiple times in the ASP program.
Step 2: Design the ASP program.
-------------------
<%@Language=JScript%>
<!--#include file=""-->
<%
var tpl = new Template("c:\\Inetpub\\wwwroot");
var str="";
var i;
("Main","");
("Main");
= 0;
for(i=1;i<=;i++) //Define as 10 in the template.
{
= i;
= i*i;
str+=("ROW");
++;
}
= str;
=""; //Clear this template variable to avoid being displayed.
%>
<%=("Main")%>
-------------------
The above program will display a square table of 1 to 10.
Usually when using templates, just add a statement to the last line to display the page. 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. This 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 Class */
/* Author: */
/* Date: 6-09 */
/*********************************************************/
//Template Method Define
function Template_Parse(name)
{
if(this[name]==null)
return "";
var reg = new RegExp("{(\\w*)}","ig");
var str = new String(this[name]);
var arr = (reg);
var i;
if(arr != null)
for(i=0;i<;i++)
{
key = (1,-1);
reg = new RegExp(arr,"ig");
if(this[key]!=null)
str = (reg,this[key]);
}
return str;
}
function Template_Split(name)
{
var len = 0;
var arr;
if(this[name]==null)
return;
var Template_Exp = new RegExp("<!--#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]+"}");
(arr[1]);
}
}
function Template_Load(name,filename)
{
var fso = new ActiveXObject("");
var file = (, filename);
if((file))
{
var f = (file, 1);
this[name] = ();
}
}
//Template Constructor
function Template(path)
{
//Property
= path;
//Method
= Template_Parse;
= Template_Split;
= Template_Load;
}
%>
When using ASP to create a site, there is often a situation where 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, the page layout must be designed and arranged, 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 "regex object" (RegExp), which can easily implement template functions using string objects and RegExp objects. Mu Feng used this to write a "" file, and the content of this file is attached to the end of the article. A competent reader can improve according to their needs.
The following 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: <!--#include file=""-->.
Let me first introduce the use of the Template class:
1. Create a Template object: Template(Path)
Parameters: Path (string type) The storage path of HTML template file.
Use the new operator to create a Template object.
example:
var tpl = new Template("c:\\template");
It can be used to obtain the template path in the program, or it can be changed by changing the template path.
like:
= "d:\\template";
2. Loading template file: (Name, File)
Parameters: 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:
("Main", "");
At this time, the template variable Main contains the contents of the file.
You can use it to access the template variable "Main".
example:
<%=%>
The content of the file you just read will be displayed.
3. Template Split: (Name)
Parameters: Name (string type) is a template variable name.
Decompose the sub-template in Name.
example:
First assume that the content in the above example is:
-------------------
This is the main template. Next is: <!--#TPLDEF SUB-->SUB sub-template, and
<!--#TPLDEF THIRD-->THIRD Template. <!--#TPLEND THIRD-->
<!--#TPLEND SUB-->
-------------------
So:
("Main");
After execution, a new template variable "SUB" and "THIRD" will be generated, and their contents are statements between <!--#TPLDEF SUB--> and <!--#TPLEND SUB-->.
And the content of the "Main" template variable will also change:
The content of the content is: "This is the main template. Next is {SUB}"
The content of the content is: "SUB sub-template, and {THIRD}"
The content of the content is: "THIRD template."
The statement blocks defined by TPLDEF and TPLEND are filled with many re-nested.
4. Template processing: (Name)
Parameters: Name (string type) is a template variable.
Replace the string enclosed in the template with curly braces with the content of the template variable of the same name.
Example: Continue with the previous example
<%=("Main")%>
Show: "This is the main template. Next is the SUB sub-template, and {THIRD}"
From the example, we can see that Parse only replaces the {SUB} variable 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:
= ("SUB"); //First process the SUB variable, then process the Main variable.
(("Main"));
5. Customize template variables.
Customizing template variables is very simple, and you can directly use assignment statements to define and modify any variable:
example:
= "This is a custom variable";
= "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 an exception may occur in the program.
Here is a complete example:
Step 1: Create the Html template file first.
Here we will 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:
-----------------
<!--File name:-->
<HTML>
<TITLE>Example</TITLE>
<HEADER>
</HEADER>
<BODY>
This is a table example.
<TABLE>
<!--#TPLDEF MAXX-->10<!--#TPLEND MAXX-->
<!--...Note that a trick is used here, which is to define the MAXX template variable and assign the value to 10. -->
<TR>
<TD>X</TD><TD>X Square</TD>
</TR>
<!--#TPLDEF ROW-->
<TR>
<TD>{X}</TD><TD>{XX}</TD>
</TR>
<!--#TPLEND ROW-->
</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 <!--#TPLDEF ROW-->...<!--#TPLEND ROW--> is to define a statement block "ROW". You can repeat the "ROW" block multiple times in the ASP program.
Step 2: Design the ASP program.
-------------------
<%@Language=JScript%>
<!--#include file=""-->
<%
var tpl = new Template("c:\\Inetpub\\wwwroot");
var str="";
var i;
("Main","");
("Main");
= 0;
for(i=1;i<=;i++) //Define as 10 in the template.
{
= i;
= i*i;
str+=("ROW");
++;
}
= str;
=""; //Clear this template variable to avoid being displayed.
%>
<%=("Main")%>
-------------------
The above program will display a square table of 1 to 10.
Usually when using templates, just add a statement to the last line to display the page. 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. This 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 Class */
/* Author: */
/* Date: 6-09 */
/*********************************************************/
//Template Method Define
function Template_Parse(name)
{
if(this[name]==null)
return "";
var reg = new RegExp("{(\\w*)}","ig");
var str = new String(this[name]);
var arr = (reg);
var i;
if(arr != null)
for(i=0;i<;i++)
{
key = (1,-1);
reg = new RegExp(arr,"ig");
if(this[key]!=null)
str = (reg,this[key]);
}
return str;
}
function Template_Split(name)
{
var len = 0;
var arr;
if(this[name]==null)
return;
var Template_Exp = new RegExp("<!--#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]+"}");
(arr[1]);
}
}
function Template_Load(name,filename)
{
var fso = new ActiveXObject("");
var file = (, filename);
if((file))
{
var f = (file, 1);
this[name] = ();
}
}
//Template Constructor
function Template(path)
{
//Property
= path;
//Method
= Template_Parse;
= Template_Split;
= Template_Load;
}
%>