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

Source: Internet
Author: User
Tags define header html page implement include variables string split
Programs | scripting | templates | Web 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} "

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 how do you fully display the "Main" template?

Example:
Tpl. SUB = TPL. Parse ("SUB"); Handle the sub variable first, then 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 template used in the "Tplpath", "Load", "Parse", "Split" variable is used internally, do not move it to use, otherwise the program will be likely to occur abnormal.

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
-----------------
<!--filename:test.htm-->
<HTML>
<TITLE> Example </TITLE>
<HEADER>
</HEADER>
<BODY>
This is a tabular example.
<TABLE>
<!--#TPLDEF maxx-->10<!--#TPLEND maxx-->
<!--... Note that a technique is used to define the Maxx template variable and assign a value of 10. -->
<TR>
Square of <td>x</td><td>x </TD>
</TR>
<!--#TPLDEF row-->
<TR>
<TD>{X}</TD><TD>{XX}</TD>
</TR>
<!--#TPLEND row-->
</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 the <!--#TPLDEF row-->...<!--#TPLEND row--> is to define a statement block "ROW". The "ROW" block can be repeated multiple times in an ASP program.

Step two: Design ASP program.

TEST. Asp
-------------------



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.