In order to avoid the situation of the ASP program and HTML code mix and write, this paper introduces a method that uses templates to separate procedures and pages, making programming easier.
When using the ASP to make a site, there is often an ASP file, and the program code and HTML code are mixed. There are many shortcomings in this way: 1. If you don't say programming, you need to design and process the page layout, resulting in a chaotic code confusion, irregular; Also need to change the ASP code, not easy to maintain.
So how do you avoid these troubles? The answer is to use the template file, separate the ASP code and the HTML page, and everything is solved. The use template has the following benefits: 1. You can replace the appearance of the entire site in a short period of time; 2. Make the programmer to abstract programming without contacting the HTML code; 3. Repeat the previous template.
Programs that use PHP will know that PHP has a template program (fastTemplate), and now how similar functions are implemented in ASP. Microsoft's ASP has two scripts: VBScript and JScript. They all have a "Regular Expression Object" (Regex), using string objects and regexp objects to easily implement template functions. Mu Feng wrote a "template.jscript.inc" file, which is attached to the article. Ability readers can improve according to their needs.
Here are the method of use. Since this file is written using JScript (of course, it is easy to turn to VBScript), so the default scripting language is set to JScript, ie the first line of the ASP program should be: <% @ language = jscript%>, then then Contains template program files: .
First introduce the use of the Template class: 1. Create a Template object: Template (path) Parameters: Path (String Type) HTML Template file save the path. Use the New operator to establish a Template object.
Example: VAR TPL = New Template ("C: // Template");
You can use TPL.TPLPATH in the program to get the template path, or you can change the template path through TPL.TPLPath. Such as: tpl.tplpath = "d: // template";
2. Load 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 under the HTML template path. Read file file to template variable Name.
Example: Tpl.Load ("Main", "Test.htm");
At this point, the template variable main contains the content of the file Test.htm. You can use TPL.Main to access template variable "main".
Example: <% = tpl.main%> The contents of the test.htm file that just read.
3. Template splitting: template.split (name) Parameters: Name (string type) is a template variable name. Decompose the sub-template in Name.
Example: First assume that the test.htm content in the above example is: ------------------ This is the master template. The next is: SUB Template, and Third Template. ------------------ then: tpl.split ("main"); After execution, new template variables "sub", and "third" are generated, and their content is the statement between the and . And the content of the "main" template variable also changes: "This is the main template. Next is the content of {sub}" tpl.sub: "SUB template, there is {third} "TPL.THIRD is:" Third Template. "
TPLDEF and TPLEND defined statements are filled with many nesting.
4. Template Processing: Template.Parse (Name) Parameters: Name (String Type) is a template variable. Replace the string of the template with a curly parentheses to replace the content of the same name.
Example: Continued in the case of <% = Tpl.Parse ("main")%> Display: "This is the main template. Next is the SUB child template, there is {third}"
As can be seen from the example, PARSE only replaces the {sub} variable in the "Main" template, and cannot be nestled. This is intentionally designed to increase program flexibility. So how do you complete the "main" template?
Example: TPL.SUB = TPL.PARSE ("Sub"); // Process the SUB variable and processes the main variable. Response.write (TPL.PARSE ("main");
5. Customize template variables. Custom template variables are simple, you can use assignment statements to define and modify any variables:
Example: tpl.hahaha = "This is a custom variable"; tpl.third = "change the Third variable in the original template";
It should be noted that since JSCrip is case sensitive, it must pay attention to the spelling of the big write. In general, the template variables defined in the HTML template use capital.
In addition, "TPLPATH", "LOAD", "Parse", "split" variables used in the template are internal, do not do it, otherwise the program will possibly an exception.
Let's take a complete example:
Step 1: Build an HTML template file first.
Here first explain the composition of the HTML template file. First, it is almost different from the ordinary HTML file, but only a few markers. There are two tags of the template. Let's take a look at an example:
Test.htm -----------------