Author: Sun Li Email: 17bizat126.com
Written on: 2006/3/5
Copyright statement: You can reprint it at will. Please mark it as a hyperlink during reprinting. Article Source and author information and this statement
Http://sunli.cnblogs.com/archive/2006/03/05/343095.html
Keywords: ASP Template
Abstract: describes a brand new ASP template engine to implement Code (Logic) layer and HTML (Presentation) layer separation. this template implementation method avoids the waste of resources such as loading template files (loading components) and replacing them with ASP templates, implements a compiled template engine, and improves Program Execution speed and stability.
Abstract: This article describes a brand new ASP template engine that separates the code (logic) layer from the HTML (Presentation) layer. This template implementation method avoids loading template files (loading
Components) and replace the wasted resources to implement the compiled template engine and improve the execution speed and stability of the program.
Content:
At present, web development has become very popular, because of various applications, it has been about to require the separation of the presentation layer and the logic layer. adding ASP and HTML together will make it difficult to maintain and make it less readable. In the PHP field, the template engine is already very common, such as phplib, smarty, and so on. there are replacement methods and compilation methods (smarty), which are good for separating the logical layer and the presentation layer. due to the influence of PHP, some people in the ASP community developed ASP template classes using replacement methods such as phplib. Because ASP is not very powerful in character processing, therefore, the speed is affected. Such templates are not widely used at present. For example:
1 <! --Template.html -->
2 <HTML>
3 4 <title >{$ title} </title>
5 6 <body>
7 {$ body}
8 </body>
9 1 <! -- Template. asp -->
2 <%
3 templatecode = load ("template.html") 'udf, load the template file to templatecode
4 templatecode = Replace (templatecode, "{$ title}", "ASP template engine Terminator") 'Replace the Template
5 templatecode = Replace (templatecode, "{$ body}", "ASP template engine Terminator content") 'Replace the Template
6response. Write templatecode
7%> the above example only shows the idea of the current ASP template. Some CMS systems of ASP edition have embedded logical control over the template. Although the logic and interface can be separated, however, this kind of template has the problem that the template needs to be parsed every time using ASP. The program is equivalent to parsing twice, and when there are many content to be replaced, the performance will be reduced. in addition, the server must support one type of component (FSO, ADODB, and XMLHTTP) to load the template ).
For reference to compiled templates, I introduced this idea in ASP and proposed a template system with excellent functions and performance in ASP. The following code is used:
1 <! --Template.html -->
2 <HTML>
3 4 <title> <% = title %> </title>
5 6 <body>
7 <! -- <%
8' if logic control, of course, this process is the same for the for and while loops, isn't it easy,
9' HTML comment on the logic. In DW (Dreamweaver), no deformation should be generated for the ASP placeholder controlled by the logic. An ASP flag will be displayed for other variables for ease of modification.
10 if catalog = "Music" then
11%> -->
12 <% = music %>
13 <! -- <% Else %> -->
14 <% = book %>
15 <! -- <% End if %> -->
16 </body>
17 1 <! -- Template. asp -->
2 <%
3 Title = "ASP template engine Terminator"
4 catalog = "music"
5 music = "music"
6 book = "book"
7%>
8 <! -- # Include file = "template.html" --> idea: Perform regular logic processing and operations on ASP files without the need for the display layer, of course, the variables to be displayed must be combined with the display layer (the same is true for PHP). In the template file, <%> is used to directly control the display and logic of variables, HTML annotator is used for non-displayed logical controllers <! ----> Comment out. Of course, it is okay not to comment out. This way, at the end of the ASP file, "<! -- # Include file = "template.html" --> "realizes the combination of templates and ASP files, achieves the separation of code and presentation layer, and does not use ASP to load templates here, then, it will waste unnecessary ASP resources. all of these processes are saved. You will find it easier to write ASP files, because you no longer need to control the replacement and display the logic. The direct execution in ASP must be faster and more stable than the replacement. Moreover, you must load a component when loading the template.
Here, you may understand the essence of this template. It is just a design model, not a template engine that uses a template class for processing.