Separation of Asp code and Page Templates page 1/3

Source: Internet
Author: User

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. Design and orchestrate the page layout during programming, resulting in confusing code and non-standard code;
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 regular expression 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 :! # Includefile = 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:
The following is a reference clip:
Vartpl = newTemplate (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:
The following is a reference clip:
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:
The following is a reference clip:
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:
The following is a reference clip:
% = 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:
The following is a reference clip:
Assume that the content of TEST. HTM in the above example is:
-
This is the main template. Next :! # TPLDEFSUBSUB sub template, and
! # TPLDEFTHIRDTHIRD template .! # TPLENDTHIRD
! # TPLENDSUB
-

So:
The following is a reference clip:
Tpl. Split (Main );

After the execution, a new template variable SUB and THIRD will be generated. Their contents are! # TPLDEFSUB and! # Statements between TPLENDSUB.
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 template, and {THIRD}
The content of tpl. THIRD is the 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

The following is a reference clip:
% = Tpl. Parse (Main) %

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.