ASP Web page template application: Let the program and interface separation, so that the ASP script clearer, change the interface easier _ Application skills
Source: Internet
Author: User
In order to avoid the maintenance difficulty of ASP program and HTML code mix writing, this paper introduces a method, using template to separate programs and pages, making the program design easier.
When using ASP to make a site, there is often a mixture of program code and HTML code in an ASP file. There are many disadvantages to doing this:
1. And do not say programming to the page layout design and choreography, resulting in code confusion difficult to understand, not standardized;
2. When you need to change the appearance of the page, you will not only change the HTML part, but also need to change the ASP code, difficult to maintain.
So how do we avoid these problems?
The answer is to use the template file, the ASP code and HTML page apart, all the problems are resolved. Using templates has the following benefits:
1. The appearance of the entire site can be replaced in a very short period of time;
2. Enable programmers to abstract programming without having to touch HTML code;
3. You can reuse the previous template.
The program that uses PHP will know that PHP has a template program (fasttemplate), the problem now is how to implement similar functions in ASP.
Microsoft's ASP comes with two kinds of scripts: VBScript and JScript. They all carry a "regular Expression object" (REGEXP) that makes it easy to implement template functionality with string objects and RegExp objects. Mu Maple to write a "Template.JScript.INC" file, the contents of this file attached to the post. The ability of the reader can be based on their own needs to improve.
Here's how to use this. Because this file is written using JScript (which is easy to turn into VBScript, of course), the default scripting language is to be set to JScript, that is, the ASP Cheng line should be: <% @Language =jscript%>, and then include the template program file: <!--#include file= "Template.JScript.INC"-->.
Let's introduce the use of the template class:
1. Establishment of Template objects: Template (Path)
Parameter: Path (string type) where the HTML template file is stored.
Use the new operator to create the template object.
Example:
var TPL = new Template ("C:\\template");
You can use TPL in your program. Tplpath to get the template path, or you can pass the TPL. Tplpath to change the template path.
Such as:
Tpl. Tplpath = "D:\\template";
2. Mount template file: Template.load (Name, file)
Parameter: Name (String type) is a template variable name.
File (string type) template filename. This file is stored under the HTML template path.
Read file to template variable name.
Example:
Tpl. Load ("Main", "TEST.") HTM ");
At this point, the template variable main contains the contents of the file test.htm.
You can use TPL. Main to access the template variable "Main".
Example:
<%=tpl. Main%>
The contents of the Test.htm file that you just read in will be displayed.
3. Template split: Template.split (Name)
Parameter: Name (String type) is a template variable name.
Decomposes the child template in name.
Example:
Let's assume that the test.htm content in the previous example is:
-------------------
This is the main template. Next is the:<!--#TPLDEF sub-->sub template, and
<!--#TPLDEF Third-->third template. <!--#TPLEND third-->
<!--#TPLEND sub-->
-------------------
So:
Tpl. Split ("Main");
After execution, the new template variable "SUB" and "third" are generated, and their contents are the statements between the <!--#TPLDEF sub--> and <!--#TPLEND sub-->.
and the contents of the "Main" template variable will also change:
Tpl. The main content is: "This is the main template." Next is {SUB} "
Tpl. The contents of the sub are: "Sub child templates, and {third}"
Tpl. The third content is: "Third template." "
The TPLDEF and tplend defined statement blocks are filled with multiple nested sets.
4. Template processing: Template.parse (Name)
Parameter: Name (String type) is a template variable.
The strings enclosed in curly braces are replaced with the contents of the template variable with the same name.
Example: continuation of the example
<%=tpl. Parse ("Main")%>
Show: "This is the main template." Next is the sub child template, and {third} "
As can be seen from the example, parse replaces only the {SUB} variable in the "Main" template instead of nesting it. This is deliberately designed to increase the flexibility of the program. So how do you fully display the "Main" template?
Example:
Tpl. SUB = TPL. Parse ("SUB"); Handle the sub variable first, then the main variable.
Response.Write (TPL. Parse ("Main"));
5. Custom template variables.
The custom template variable is simple enough to define and modify any variable directly with an assignment statement:
Example:
Tpl. hahaha = "This is a custom variable";
Tpl. Third = "Change the third variable in the original template";
It's important to note that because Jscrip is case-sensitive, be sure to pay attention to the spelling of the case. In general, template variables defined in HTML templates are capitalized.
In addition, the template used in the "Tplpath", "Load", "Parse", "Split" variable is used internally, do not move it to use, otherwise the program will be likely to occur abnormal.
Here's a complete example:
First step: Create the HTML template file first.
Here we first describe the composition of the HTML template file. First, it's almost indistinguishable from the normal HTML file, just a few more tags.
There are two types of markup for a template. Let's take a look at the first example:
TEST. Htm
-----------------
<!--filename:test.htm-->
<HTML>
<TITLE> Example </TITLE>
<HEADER>
</HEADER>
<BODY>
This is a tabular example.
<TABLE>
<!--#TPLDEF maxx-->10<!--#TPLEND maxx-->
<!--... Note that a technique is used to define the Maxx template variable and assign a value of 10. -->
<TR>
Square of <td>x</td><td>x </TD>
</TR>
<!--#TPLDEF row-->
<TR>
<TD>{X}</TD><TD>{XX}</TD>
</TR>
<!--#TPLEND row-->
</TABLE>
There is a total of {COUNT} row data above.
</BODY>
</HTML>
-----------------
As you can see from the above, tokens like {x},{xx},{count} are defined as template variables. They will be replaced in the ASP program.
And the <!--#TPLDEF row-->...<!--#TPLEND row--> is to define a statement block "ROW". The "ROW" block can be repeated multiple times in an ASP program.
Step two: Design ASP program.
TEST. Asp
-------------------
<% @Language =jscript%>
<!--#include file= "Template.JScript.INC"-->
<%
var TPL = new Template ("C:\\Inetpub\\Wwwroot");
var str= "";
var i;
For (I=1;I<=TPL. maxx;i++)//tpl. Maxx is defined in the template as 10.
{
Tpl. X = i;
Tpl. XX = I*i;
Str+=tpl. Parse ("ROW");
Tpl. count++;
}
Tpl. ROW = str;
Tpl. MAXX = ""; Empty this template variable to avoid being displayed.
%>
<%=tpl. Parse ("Main")%>
-------------------
The above program will display a square table of 1 to 10.
Typically, when you use a template, you just add the statement that displays the page to the last line. So the whole procedure seems very clear. At this point, as long as you edit the template file, you can change the appearance of the entire page.
As for the template file, it can be any file, such as HTML file, ASP file, even the program itself!, and in a program can be loaded with multiple templates to use, so that not only a great deal of flexibility, and template files and ASP program relevance can be minimized.
Making good use of the template will make your job easier.
Attachment: Template source program
------------------------------------
<!--filename:template.jscript.inc-->
<%
/*********************************************************/
/* Template Class * *
* Author: Mu Feng (lin.y@263.net) * *
* date:2000-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 = Str.match (reg);
var i;
if (arr!= null)
for (i=0;i<arr.length;i++)
{
Key = Arr[i].slice (1,-1);
reg = new RegExp (Arr[i], "IG");
if (this[key]!=null)
str = str.replace (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);
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.