ASP web template application: Let the program and interface separation, let the ASP script clearer, and the replacement interfac

zhaozj2021-02-17  68

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 -----------------

This is a Table sample.

- # TPLDEF MAXX -> 10
x x square
{x} {xx} The above has a total of {count} line data. ---------------- From the above, you can see that the marker like {x}, {xx}, {count} is to define template variables. They will be replaced in the ASP program. And is defined a statement block "Row". The "ROW" block can be repeated multiple times in the ASP program. Step 2: Design an ASP program. Test.asp ------------------- <% @ language = jscript%> <% VAR TPL = New Template ("c: // inetpub // wwwroot"); var str = ""; var i; tpl.load ("main", "test.htm"); TPL.SPLIT ("main") ; Tpl.count = 0; for (i = 1; i <= tpl.maxx; i ) //tpl.maxx is defined in the template. {TPL.X = I; TPL.XX = i * i; str = tpl.parse ("row"); tpl.count ;} TPL.ROW = Str; TPL.MAXX = ""; // Empty this template variable To avoid being displayed. %> <% = TPL.PARSE ("Main")%> ----------------- The above program will display a square table of 1 to 10. Usually in the case of using a template, as long as the statement is displayed in the last line. Therefore, the entire program is very clear. At this time, as long as you edit the template file, you can change the appearance of the entire page. As for the template file, it can be any file, such as HTML file, ASP file, or even the program itself! The correlation of the program can be minimized. Use the template to make your work easier.

Attached: Template source -------------------------------------! - File name: template. JScript.inc -> <% / ************************************************ **************** / / * TEMPLATE CLASS * / / / * Author: Mu Feng (lin.y@263.net) * // * Date: 2000-6-09 * // ******************************************************** ********* / // Template Method Define Function Template_Parse (Name) {IF (this [Name] == Null) Return ""; varreg = new regexp ("{(// w *)} "," iz "); var str = new string (this [name]); var arr = str.match (reg); var i; if (arr! = null) for (i = 0; i ((. | - # TPLEND / / 1 * ->, "i"); while (this [name] .Search (Template_exp)! = - 1) {Arr = this [name] .match (Template_exp); this [arr [1]] = arr [2]; this [name] = this [name] .REPLACE (Template_exp, "{ " Arr [1] "} "); this.Split (Arr [1]);}}} function template_load (name, filename) {var fso = new activXObject (" scripting.filesystemObject "); var file = fso. Buildpath (THIS.TPLPATH, FILENAME); if (fso.fileexists (file)) {var f = fso.opentextfile (file, 1); this [name] = f.readall ();}}

转载请注明原文地址:https://www.9cbs.com/read-31337.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.046, SQL: 9