An ASP template class

Source: Internet
Author: User
Tags end variables variable
Template An example of a template from a class to an instance
This class still has a lot of imperfections (such as efficiency), I hope we discuss together, master a lot of correct.
-----------------------------------------------------------------------
First, introduce the classes in asp:
Class object that requires version 5.
Like some object-oriented languages, the ASP also provides support for classes, but is functionally imperfect.
For the description used in this class:
Initialize Event: This event occurs when an instance of a class is created.

Private Sub Class_Initialize ()
Statement
End Sub


This event is similar to a constructor in C + + to construct an object that initializes the properties of some classes in this class.
How do I create an instance?

Dim X
Set X = New classname


At this point x is already an object of the Class_Initialize process.
A function (procedure) defined in a class is called a method of a class.
Specific details can be referred to:
Mytju.com/classfiles/tju001/vbs5.5.chm
-----------------------------------------------------------------------
Introduction to Template technology:

When you write a program (such as a message board), have you ever experienced the way of looping + tables?
This written program, both poor readability, and difficult to maintain code.
For example, to change the color of a table in the future, or to change the layout of the page (for example, change the location of the message),
Maybe all the code will be rewritten (the message board of course not, the big news system is troublesome).
And the purpose of template technology is to:
The Presentation Layer (page) is separated from the logical layer (code).
This is a paging file (that is, a template):

The following are the effects that have been resolved by the code file:

Perhaps you would ask, what about this? No more trouble?
Perhaps in the early writing, will increase the cycle, however, the latter as long as you replace the template file, is a
It's a brand-new result! In this process, the ASP's VBS script file is not subject to modification at all.
Further: The above parsing process is controlled by the VBS you write, so (theoretically) it can be parsed into
Any type of file, as long as there is a template!
So what do you think? Yes, Sina you can do it too! (exaggerated rhetoric) before making a news system,
The era of news.asp?id=xxx (read news by News ID) can be over, and you can generate static pages:
2004090618.htm.
To be honest, say template technology:
Template technology is simply to put the [template file] inside the variable to be replaced (here is the contents of {}),
Replace it with what you need to display.
Ordinary variable substitution is easy: you only need to understand the operation of the FSO (AD show: Cnbruce has an ASP tutorial, detailed explanation)
It's easy to read all the contents of the template file and replace the ' tag ' that you want to replace (replace ())
Implement the effect.
And the core of the template technology should be the processing of the loop:
Some of the techniques that you've seen before are using a custom label and then adding a decision loop to the tag
Some variables, such as name, number.
Personally think this may violate the original intention of the template--the separation of presentation layer and logic layer
And the efficiency of the regular is also not flattering.
The number of loops should be controlled entirely by code, rather than by making the page person (art) to add.
So I used the idea of setting blocks: (Borrowed from PHP)
To cycle the page code, as a block, the entire block and then as a page variable to deal with.

See figure below:

All you need to do is add an HTML annotation (similar to the label above)
And in the Code section: (TP is an object of class, Set_block,set_bvar is a method of Class)

' The way the loop is handled

Tp.set_block "B" to set blocks, corresponding to block B in the graph
For Tmpc=1 to 10 ' Set the number of cycles, depending on the program
Tp.set_bvar "T1", "Alax" &TMPC ' parsing variables in blocks
Tp.set_bvar "T2", "ProA" &TMPC
Tp.set_bvar "T3", "HOTSJF" &TMPC
Tp.prase_block (10-TMPC) ' to see the block as a variable to parse the whole block
Next



You need an instance to download this.
Www.7csky.com/user/hotsjf/hottemplate.rar
Run test.asp to see the effect of using t.htm as a template.
Effect Demo Point this:
Code files:
202.4.136.222:802/test.asp
Template file:
202.4.136.222:802/t.htm


The next thing is to show the template content that has replaced all the tags.
Display can directly response.write the content
You can also use the FSO to write content as a file, and you can generate other types of files (such as. htm).


Thank you for your comments.

Simple summary of the characteristics of the template:
The presentation layer is separate from the logical layer, and the page staff does not need to consider the logical part.
Blocks (block) of the idea, to solve the ASP easily caused by the intersection of the page and code parts.
With caching to speed up file generation.
= =



Template code: Reference Please keep the information, thank you.
Class: Hottemplat.asp

<%
'======================================'
' Hottemplate
' By HOTSJF
' 2004.7.28
' Sjf008@tom.com
' Some rights reserved
'======================================'

On Error Resume Next

Class hottemplate
' Class member

Dim file

Dim Var_key (50)
Dim Var_val (50)
Dim var_num

Dim Bvar_key (50)
Dim Bvar_val (50)
Dim bvar_num

Dim block_name
Dim block_content
Dim New_block

Dim debug
Dim Clear_unknown_tag

Dim Beginblockstart

Dim beginblockend

Dim Endblockstart

Dim endblockend

Dim varbegin
Dim varend


'-------------------------------------------------------------------'
' Initialization
Private Sub Class_Initialize
Var_num=0
Bvar_num=0
Debug=false
Beginblockstart= "<!--begin Block"
Beginblockend= "-->"
Endblockstart= "<!--end Blocks"
Endblockend= "-->"
Varbegin= "{"
Varend= "}"
File= ""
Clear_unknown_tag=true
If Debug Then
Response.Write ("in Class_Initialize () <br>")
End If

End Sub
'-------------------------------------------------------------------'
' Load template file
Public function loadfile (filename)
Set fso = CreateObject ("Scripting.FileSystemObject")
Set f = fso. OpenTextFile (Server.MapPath (filename), 1)
If Err.number>0 Then
Exp_msg ("No file matched!")
End If
File = F.readall
' Write into cache
Application.Lock
Application ("HOT_TP" &filename) =file
Application.UnLock
If Debug Then
Response.Write ("I



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.