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. This is a lot of disadvantages: 1. If you don't say programming, you need to design and process the page layout, causing code confusion, irregular; 2. When you need to change the page appearance, you must not only change the HTML part, 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. Replace the appearance of the entire site in a very short period of time; 2. Make the programmer abstract programming without having to contact 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. Establish 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 to get the template path in the program, or change the template path via 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 contents 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 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 <% = tpl.Parse ("main")%> Display: "This is the main template. Next is the SUB child template, and {third}" is known by example, Parse only replaces the "main" template The {SUB} variable is not nested. 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 directly define and modify any variables directly: TPL.haha = "This is a custom variable"; tpl.third = "change the Third variable in the original template"; need to pay attention Yes, 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 -----------------