Code and page Separation

Source: Internet
Author: User

To avoid maintenance difficulties caused by ASP and HTML code mixed writing, this article introduces a method to use templates to separate programs and pages, making program design easier.

When using ASP to create a site, a combination of program code and HTML code often occurs in an ASP file. This method has many disadvantages:
1. When programming, we should design and orchestrate the page layout, resulting in code confusion, difficulty, and non-standardization;
2. When you need to change the page appearance, you must not only change the HTML part, but also 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 ASP code from HTML pages. All problems are solved. Using templates has the following benefits:
1. The appearance of the entire site can be replaced in a short time;
2. Enable programmers to abstract programming without having to access HTML code;
3. You can reuse the previous template.

PHP has a template program (FastTemplate), and the question is how to implement similar functions in ASP.
Microsoft ASP has two types of scripts: VBScript and JScript. They all contain a "RegExp object" (RegExp). Using string objects and RegExp objects, you can easily implement the template function. Mu Feng wrote a "Template. JScript. INC" file. The content of this file is attached to the end of the article. Competent readers can make improvements based on their own needs.

The following describes how to use it. Because this file is written in JScript (of course, it is easy to convert it to VBScript), the default script language should be set to JScript, that is, the first line of the ASP program should be: <% @ Language = JScript %>, and then include the template program file: <! -- # Include file = "Template. JScript. INC" -->.

First, we will introduce the usage of the Template class:
1. Create a Template object: Template (Path)
Parameter: Path (string type) Storage Path of the HTML template file.
Use the new operator to create a Template object.

Example:
Var tpl = new Template ("c: \ template ");

In the program, you can use tpl. TplPath to obtain the template path, or you can use tpl. TplPath to change the template path.
For example:
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 to the template variable Name.

Example:
Tpl. Load ("Main", "TEST. HTM ");

In this case, 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 that you just read is displayed.

3. Template Splitting: Template. Split (Name)
Parameter: Name (string type) is a template variable Name.
Break down sub-templates in Name.

Example:
Assume that the content of TEST. HTM in the above example is:
-------------------
This is the main template. Next: <! -- # Tpldef sub --> SUB template, and
<! -- # Tpldef third --> THIRD template. <! -- # Tplend third -->
<! -- # Tplend sub -->
-------------------
So:
Tpl. Split ("Main ");
After the execution, a new template variable "SUB" and "THIRD" will be generated. Their content is <! -- # Tpldef sub --> and <! -- # Statements between tplend sub -->.
The content of the "Main" template variable also changes:

The content of tpl. Main is: "This is the Main template. Next is {SUB }"
The content of tpl. SUB is: "SUB subtemplate, and {THIRD }"
The content of tpl. THIRD is: "THIRD template. "

The statement blocks defined by TPLDEF and TPLEND are nested many times.

4. Template processing: Template. Parse (Name)
Parameter: Name (string type) is a template variable.
Replace the string enclosed in curly braces in the template with the content of the template variable with the same name.

Example: renewal example
<% = Tpl. Parse ("Main") %>
Show: "This is the main template. Next is the SUB-template, and {THIRD }"

The example shows that Parse only replaces the {SUB} variable in the "Main" template, but cannot be replaced by nested variables. This was designed to increase program flexibility. How can I display the "Main" template in full?

Example:
Tpl. SUB = tpl. Parse ("SUB"); // process the SUB variable before the Main variable.
Response. write (tpl. Parse ("Main "));

5. Customize template variables.
Custom template variables are simple. You can use the assign value statement to define and modify any variables:

Example:
Tpl. Hahaha = "this is a custom variable ";
Tpl. THIRD = "changing the THIRD variable in the original template ";

Note that JScrip is case sensitive, so you must note the spelling of the case. Generally, the template variables defined in the HTML template are in uppercase.

In addition, the "TplPath", "Load", "Parse", and "Split" variables used in the template are used internally. Do not use them. Otherwise, an exception may occur in the program.

The following is a complete example:

Step 1: Create an Html template file first.

The following describes the composition of HTML template files. First, it is almost the same as a common HTML file, but it only has a few more tags.
There are two types of template tags. Let's take a look at an example:

TEST. HTM
-----------------
<! -- File name: TEST. HTM -->
<HTML>
<TITLE> example </TITLE>
<HEADER>
</HEADER>
<BODY>
This is a table example.
<TABLE>
<! -- # Tpldef maxx --> 10 <! -- # Tplend maxx -->
<! --... Note that a technique is used to define the MAXX template variable and assign the value to 10. -->
<TR>
<TD> X </TD> <TD> the square of X </TD>
</TR>
<! -- # Tpldef row -->
<TR>
<TD> {X} </TD> <TD> {XX} </TD>
</TR>
<! -- # Tplend row -->
</TABLE>
The preceding table contains {COUNT} rows of data.
</BODY>
</HTML>
-----------------

From the above, we can see that marks such as {X}, {XX}, and {COUNT} define template variables. They will be replaced in ASP programs.
And <! -- # Tpldef row -->... <! -- # Tplend row --> define a statement block "ROW ". In ASP, you can repeat the "ROW" Block multiple times.

Step 2: design the ASP program.

TEST. ASP
-------------------
<% @ Language = JScript %>
<! -- # Include file = "Template. JScript. INC" -->
<%
Var tpl = new Template ("c :\\ Inetpub \ wwwroot ");
Var str = "";
Var I;

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 the template variables to avoid being displayed.
%>
<% = Tpl. Parse ("Main") %>
-------------------
The above program will display a square table ranging from 1 to 10.

When you use a template, you only need to add the statement that displays the page to the last line. Therefore, the entire program is very clear. In this case, you only need to 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 file, ASP file, or even the program itself !, In addition, multiple templates can be loaded in a program for use. This not only provides great flexibility, but also minimizes the relevance between template files and ASP programs.
Making good use of templates will make your work easier.

Appendix: Template source program
------------------------------------
<! -- File name: Template. JScript. INC -->
<%
/*************************************** ******************/
/* 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 = str. match (reg );
Var I;

If (arr! = Null)
For (I = 0; I <arr. length; I ++)
{
Key = arr. slice (1,-1 );
Reg = new RegExp (arr, "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 );

This [arr [1} = arr [2];
This [name] = this [name]. replace (Template_Exp, "{" + arr [1] + "}");
This. Split (arr [1]);
}
}

Function Template_Load (name, filename)
{
Var fso = new ActiveXObject ("Scripting. FileSystemObject ");
Var file = fso. BuildPath (this. TplPath, filename );
If (fso. FileExists (file ))
{
Var f = fso. OpenTextFile (file, 1 );
This [name] = f. ReadAll ();
}
}

// Template Constructor

Function Template (path)
{
// Property
This. TplPath = path;

// Method
This. Parse = Template_Parse;
This. Split = Template_Split;
This. Load = Template_Load;
}
%>

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.