ASP an imitation Phplib template class

Source: Internet
Author: User
Tags exit html page variables php and variable valid
Templates used in PHP, used phplib used, no he felt very uncomfortable
The internet to find no one to write, their own wrote a, ASP level is not high, I hope
Can have the master to change.
<%
' ====================================
' Name:template Class
' Purpose:parse and Output HTML page
' Date:10.2002
' Author:pig
' Email:pigzjq@sina.com
' phone:13910320759
' ====================================

Class Template
Dim gs_root
Dim Gs_handle ()
Dim Gs_file ()
Dim Gs_keys ()
Dim Gs_keyvars ()
Dim Gs_vars ()

' Constructor
Private Sub template_initialize ()
Call Of_setroot (".")
' Call Of_redimvar ()
End Sub

function Of_redimvar ()
ReDim Gs_handle (0)
ReDim Gs_file (0)
ReDim Gs_keys (0)
ReDim Gs_keyvars (0)
ReDim Gs_vars (0)
End Function

' Set template Store path
function Of_setroot (ByVal ps_root)
If Ps_root <> "" Then
Gs_root = Ps_root
End If
End Function

' Set file name function
function Of_setfile (byVal ps_filehandle,byval ps_filename)
If Ps_filename <> "" Then
Li_maxnum=ubound (Gs_handle)
Gs_handle (li_maxnum) = Ps_filehandle
Gs_file (li_maxnum) = gs_root+ "/" +ps_filename

Li_maxnum=li_maxnum+1
ReDim Preserve Gs_handle (Li_maxnum)
ReDim Preserve Gs_file (Li_maxnum)
End If
End Function

' Set the parameter variables to replace
function Of_setvar (byVal ps_key,byval Ps_var)
If Ps_key <> "" and Ps_var <> "" Then
Li_keyindex=of_getindex (Gs_keys,ps_key)
If li_keyindex= "No" then
Li_maxnum=ubound (Gs_keys)
Gs_keys (Li_maxnum) =ps_key
Gs_keyvars (Li_maxnum) = "\{" & Ps_key & "\}"
Gs_vars (Li_maxnum) =ps_var

Li_maxnum=li_maxnum+1
ReDim Preserve Gs_keys (Li_maxnum)
ReDim Preserve Gs_keyvars (Li_maxnum)
ReDim Preserve Gs_vars (Li_maxnum)
Else
Gs_keys (Li_keyindex) =ps_key
Gs_keyvars (Li_keyindex) = "\{" & Ps_key & "\}"
Gs_vars (Li_keyindex) =ps_var
End If
End If
End Function

' Define overloaded text blocks
function Of_setblock (byVal ps_parent,byval ps_handle,byval ps_name)
if (not Of_loadfile (ps_parent)) Then
Ls_error= "Of_loadfile Unable to load" +ps_parent
Response.Write (Ls_error)
Of_setblock=false
Exit function
End If

If Ps_name= "" Then
Ps_name=ps_handle
End If

Ls_string=of_getvar (ps_parent)
Ls_pattern = "<!--\s*begin" &ps_handle& "\s*--> (. *) <!--\s*end" &ps_handle& "\s*-->"

Set regEx = New RegExp
Regex.pattern = "\ n"
Regex.ignorecase = False
Regex.global = True
ls_string = Regex.Replace (ls_string, "")

Regex.pattern = Ls_pattern
Regex.multiline = True
Regex.global = False
Set matches = Regex.execute (ls_string)
ls_string = Regex.Replace (ls_string, "{" &ps_name& "}")

For the Match in matches
Ls_value=match.value
Next

Regex.pattern = "<!--\s*begin" &ps_handle& "\s*-->"
Regex.ignorecase = False
Regex.global = True
Ls_value = Regex.Replace (Ls_value, "")

Regex.pattern = "<!--\s*end" &ps_handle& "\s*-->"
Regex.ignorecase = False
Regex.global = True
Ls_value = Regex.Replace (Ls_value, "")

Call Of_setvar (Ps_handle,ls_value)
Call Of_setvar (ps_parent,ls_string)
End Function

' Load variable content
function Of_loadfile (ByVal ps_handle)
Li_keyindex=of_getindex (Gs_keys,ps_handle)
If Li_keyindex = "No" Then
Li_fileindex=of_getindex (Gs_handle,ps_handle)
If Li_fileindex = "No" Then
Ls_error= "LoadFile:" +ps_handle+ "is not a valid handle."
Response.Write (Ls_error)
Of_loadfile=false
Exit function
End If

Ls_filename=gs_file (Li_fileindex)
Ls_filename=server.mappath (Ls_filename)
Set myfileobject=server.createobject ("Scripting.FileSystemObject")
Li_exist=myfileobject.fileexists (Ls_filename)
If Li_exist Then
Set Mytextfile=myfileobject.opentextfile (Ls_filename)
Ls_file=mytextfile.readall ()
Mytextfile.close
Call Of_setvar (Ps_handle,ls_file)
Else
Ls_error= "LoadFile:" +ls_filename+ "is not a valid file or path."
Response.Write (Ls_error)
Of_loadfile=false
Exit function
End If
End If

Of_loadfile=true
End Function

' Variable substitution process
Function Of_replace (ByVal ps_handle)
if (not Of_loadfile (ps_handle)) Then
Ls_error= "of_replace:unable to load" +ps_handle
Response.Write (Ls_error)
Of_replace=false
Exit function
End If

Ls_str=of_getvar (Ps_handle)
Li_minindex=lbound (Gs_keys)
Li_maxindex=ubound (Gs_keys)

For I=li_minindex to Li_maxindex
Set regEx = New RegExp
Regex.pattern = Gs_keyvars (i)
Regex.ignorecase = True
Ls_str = Regex.Replace (Ls_str, Gs_vars (i))
Next

Of_replace=ls_str
End Function

' Get storage variables
Function Of_getvar (ByVal ps_keyname)
Li_keyindex=of_getindex (Gs_keys,ps_keyname)
if (li_keyindex= "no") then
Of_getvar= ""
Else
Ls_varname=gs_vars (Li_keyindex)
Of_getvar=ls_varname
End If
End Function

' Find the index value of the corresponding string in the array
Function Of_getindex (ByVal ps_array,byval Ps_handle)
Li_minindex=lbound (Ps_array)
Li_maxindex=ubound (Ps_array)
li_index= "No"
For I=li_minindex to Li_maxindex
if (Ps_array (i) =ps_handle) then
Li_index=i
Exit For
End If
Next
Of_getindex=li_index
End Function

' Analyze variables
Function Of_parse (ByVal ps_target,byval Ps_handle,byval Ps_append)
ls_string = Of_replace (ps_handle)
If Ps_append=true Then
ls_org = Of_getvar (ps_target) & ls_string

' If pi_time=2 then
' Response.Write (ls_string)
' Exit function
' End If

Call Of_setvar (ps_target,ls_org)
Else
Call Of_setvar (ps_target,ls_string)
End If
End Function

Function Of_print (ByVal ps_keyname)
Li_keyindex=of_getindex (Gs_keys,ps_keyname)
Ls_varstring=gs_vars (Li_keyindex)

Set regEx = New RegExp
Regex.pattern = "{[^ \t\r\n}]+}"
Regex.ignorecase = True
ls_varstring = Regex.Replace (ls_varstring, "")
Of_print=ls_varstring
End Function
End Class
%>

Call Page:
<%
Dim c_html
Set c_html = new Template

Call C_html.of_redimvar ()
Call C_html.of_setfile ("main", "index.htm")
Call C_html.of_setblock ("main", "Block_test", "MyTest")

Call C_html.of_setvar ("TEST", "Testblock1")
Call C_html.of_parse ("MyTest", "Block_test", True)
Call C_html.of_setvar ("TEST", "Testblock2")
Call C_html.of_parse ("MyTest", "Block_test", True)

Call C_html.of_setvar ("TEST1", "success")
Call C_html.of_parse ("Main_output", "MAIN", false)
Ls_result=c_html.of_print ("Main_output")
Response.Write (Ls_result)
%>

Template file:
{TEST1}
<table border=1>
<!--BEGIN Block_test-->
&LT;TR&GT;&LT;TD width= ' class= ' H1 ' height= ' bgcolor= ' #f0f0f0 ' align= ' center ' >
{TEST}
</td></tr>
<!--end Block_test-->
</table>

Hope to be helpful to friends who used the powerful features of Phplib.
I want to transfer all the PHP and ASP enthusiasts
My qq:9457009.

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.