Discussion: The separation of Code and page in ASP

Source: Internet
Author: User
Tags define count header html page implement variables split variable
Page

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), using string objects and RegExp objects to easily implement template functionality. Mu Maple to write a Template.JScript.INC file, the content of this file is 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 in JScript (which is easy to turn into VBScript, of course), the default scripting language is set to JScript, that is, the ASP Cheng line should be:% @Language =jscript%, and then include the template program file:!# Includefile=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:
Vartpl=newtemplate (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:! #TPLDEFSUBSUB子模板, there are
! #TPLDEFTHIRDTHIRD模板.! #TPLENDTHIRD
! #TPLENDSUB
-

So:
Tpl. Split (Main);
After execution, a new template variable sub, and third, are generated, and their content is! #TPLDEFSUB和! #TPLENDSUB之间语句.
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 what about the full display of the main template?
Example:
Tpl. Sub=tpl. Parse (sub);//Handle the sub variable first, then handle 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 tplpath,load,parse,split variable used in the template is used internally, do not move it, or the program will have an exception.
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

-
! File Name: test.htm
Html
TITLE Example/title
HEADER
/header
Body
This is a tabular example.
TABLE
#TPLDEFMAXX10! #TPLENDMAXX
!... Note that a technique is used to define the Maxx template variable and assign a value of 10.
Tr
The square/td of TDX/TDTDX
/tr
! #TPLDEFROW
Tr
Td{x}/tdtd{xx}/td
/tr
! #TPLENDROW
/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 #TPLDEFROW ...! #TPLENDROW是定义一个语句块ROW. You can repeat a row block multiple times in an ASP program.

Step two: Design 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 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

! 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;
}
%



Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.