Application of ASP Web page template

Source: Internet
Author: User
Tags html page regular expression split

ASP Web page Template application: Lets the program and the interface separation, lets the ASP script be clearer, the replacement interface is easier

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:

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:! #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.

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.