The article that the programmer looks at is also about page and code separation

Source: Internet
Author: User
Tags html page implement include regular expression split
Program | programmer | Code Separation | page in order to avoid the maintenance difficulties of ASP program and HTML code, this paper introduces a method, using templates to separate programs and pages, making programming 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%&gt, 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} "

&nb



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.