Example detailed explanation ASP generation static Page method

Source: Internet
Author: User
Tags chr html page modify newsfile reference reset

1, with templet meaning is that the generated page schema will take a set of templates, which I introduced in a tutorial before, I hope you see this tutorial on the ASP template should be familiar with.

2, ASP converted to HTML. Don't let me talk about the benefits of ASP's transformation into HTML, the most worth knowing is that static HTML pages and dynamic pages are much less demanding on the server, and that static HTML searches are far more likely than dynamic pages.

So the technical problem I need to deal with right now is:

1, how to implement template technology?
2, how to achieve 2HTML technology?
3. How to combine template technology with 2HTML technology?

   first, the technical principle analysis

1, the template technology refer to
2, how to make the ASP page into HTML? The FSO component is generally thought of, because the component can create any new file format.

So what is the whole process of running?

A, the provision of information input page for information collection;
b, the acceptance information value first save the database, then the FSO to generate files;
C, technical completion of the task to display the newly created HTML file path address. There are several difficulties in the implementation of this technology:

I, the FSO generated files are directly placed in a large folder, or in a separate daily update subfolder? may be inaccurate, so understand: believe that the file generated through the FSO over time, the file will be more and more, management will be more and more chaotic ... Usually you may see some addresses such as www.xxx.com/a/2004-5-20/200405201111.html can be analyzed to create the current date of the folder. In this way, a day is a folder page content, view management also seems more reasonable.

II, when I tried to create a folder through the above method, I found a second problem. The first time through the FSO to create a folder named after the current date, no problem. When I have a new file to build, because it is the same program, it will execute the same folder. At this point, the FSO component will find that the path already exists ... Jam-_-! Continue processing, add code in the first line: on Error Resume Next, to deceive oneself, the effect of cheating.

Of course, the use of the rules is to determine whether the folder has no:
<%
Set FSO = Server.CreateObject ("Scripting.FileSystemObject")
if (FSO). FolderExists (Server.MapPath (folder)) then
' Judge if there's no deal.
Else
' Determine if a new folder is not present
Fso. CreateFolder (Server.MapPath (folder))
End If
%>
III, the folder is established, how to establish the document? This is mainly the generation of file names. Of course, this will need to write a function of its own, the function is how to generate file name:
<%
function Makefilename (fname)
fname = FName ' before fname as variable, after fname for function parameter reference
fname = replace (fname, "-", "")
fname = replace (fname, "", "")
fname = replace (fname, ":", "")
fname = replace (fname, "PM", "")
fname = replace (fname, "AM", "")
fname = replace (fname, "a.m.", "")
fname = replace (fname, "PM", "")
Makefilename = fname & ". html"
End Function
%>

Reference function: <%fname = Makefilename (now ())%>

In fact, is the date of the day and the minutes of the name of the file.

IV, and finally, how are the generated files viewed? Of course, you need to save the path to the generated file in the database and add it to the corresponding set of records. Of course, this is mentioned in the following database design.

3. Combination of template technology and 2HTML technology: Replace the value of special code in the template with the values received from the form, complete the template function, and generate HTML files for all the template codes that are eventually replaced. Note that the substitution should be able to completely change the format of the input data or the code that supports UBB.

   second, the database design

At present, database design needs two tables: one is to store the template data, one is to store the information content. 1, create a new database Asp2html.mdb

2. Design a new database table C_moban

Field m_id (AutoNumber, primary key), Field m_html (Memo type).

and copy the following complete code to the M_html field
<meta http-equiv= "Content-type" content= "text/html; Charset=hz ">
<title>cnbruce.com | Asp2html test</title>
<body leftmargin= "0" topmargin= "0" >
<table width= "100%" height= "100%" border= "0" cellpadding= "5" cellspacing= "2" >
<tr align= "Right" bgcolor= "#CCCCCC" >
&LT;TD height= colspan= "2" > $cntop {logcontent}lt;/td>
</tr>
&LT;TR valign= "Top" >
&LT;TD width= "25%" bgcolor= "#e5e5e5" > $cnleft {logcontent}lt;/td>
&LT;TD width= "74%" bgcolor= "#f3f3f3" > $cnright {logcontent}lt;/td>
</tr>
</table>
</body>
3. Design a new database table C_news

Field c_id: Automatic numbering, primary key
Field C_title: Text type, saving article title
Field C_content: Memo type, saving article content
Field C_filepath: Text type, keeping the path address of the generated file
Field C_time: Date/Time type, default value: Now ()

   third, page requirements design

1, first set up a folder to store HTML pages

In the same directory, set up folder Newsfile, clips inside the main storage generated HTML pages, of course, the internal will also adopt a program to establish a date-named subfolder to facilitate browsing and management.

2, Function function page lib.asp
<%
' function to generate file name
function Makefilename (fname)
fname = fname
fname = replace (fname, "-", "")
fname = replace (fname, "", "")
fname = replace (fname, ":", "")
fname = replace (fname, "PM", "")
fname = replace (fname, "AM", "")
fname = replace (fname, "a.m.", "")
fname = replace (fname, "PM", "")
Makefilename=fname & ". sHTML"
End Function

' A function that keeps the data format unchanged
function HTMLEncode (fstring)
fstring = replace (fstring, ">", ">")
fstring = replace (fstring, "<", "<")
fstring = Replace (fstring, CHR (32), "")
fstring = Replace (fstring, CHR (13), "")
fstring = Replace (fstring, CHR (a) & CHR (a), "<br>")
fstring = Replace (fstring, CHR (), "<br>")
HTMLEncode = fstring
End Function
%>
3. Database Connection Page conn.asp

Complete the string concatenation method of the database
<%
Set conn = Server.CreateObject ("ADODB. Connection ")
ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" &server.mappath ("Asp2html.mdb")
Conn. Open ConnStr
%>
4, Information input page add.html

It's really simple, it's just a form. Note that the action is to jump to addit.asp
<form action= "addit.asp" method= "POST" >
Title:<input type= "text" name= "C_title" ><br>
Content:<br>
<textarea name= "C_content" rows= "8" cols= "></textarea><br>"
<input type= "Submit" value= "ADD" >
<input type= "reset" value= "reset" >
</form> 5, processing data function Display page addit.asp

The first is to process the received data and write the value to the database, then reference the template code and convert the special code to the accepted value, which eventually generates an HTML page through the FSO. Note also that the path address of the generated file is saved to the database table.
<% ' fault-tolerant processing
On Error Resume Next
%>

<!--#include file= "conn.asp"-->
<!--#include file= "lib.asp"-->

<% ' Accept delivery value
C_title=request.form ("C_title")
C_content=request.form ("C_content")
%>

<% ' generate HTML filename, create folder, specify file path
FName = Makefilename (now ()) ' Makefilename as a custom function
folder = "newsfile/" &date () & "/"
filepath = Folder&fname
%>

<% ' Keep the accepted values and paths to the database table
sql = "SELECT * FROM C_news"
Set rs = Server.CreateObject ("ADODB.") Recordset ")
Rs. Open sql,conn,3,2
Rs.addnew
RS ("C_title") =c_title
RS ("C_content") =c_content
RS ("C_filepath") =filepath
Rs.update
Rs.close
Set rs = Nothing
%>

<% ' Open the template code and turn the special code into accepted values
sql1= "Select m_id,m_html from C_moban where m_id=1"
Set Rs1=server.createobject ("Adodb.recordset")
Rs1.open sql1,conn,1,1
Mb_code=rs1 ("m_html")
Rs1.close
Set rs1=nothing
Conn.close
Set conn=nothing
C_title=htmlencode (C_title)
C_content=htmlencode (c_content)
Mb_code=replace (Mb_code, "$cntop {Logcontent}quot;,now ()")
Mb_code=replace (Mb_code, "$cnleft {logcontent}quot;,c_title)
Mb_code=replace (Mb_code, "$cnright {logcontent}quot;,c_content)
%>

<% ' Generate HTML page
Set FSO = Server.CreateObject ("Scripting.FileSystemObject")
Fso. CreateFolder (Server.MapPath (folder))
Set fout = fso. CreateTextFile (Server.MapPath (filepath))
Fout. WriteLine Mb_code
Fout.close
%>
Article added successfully, <a href= "showit.asp" > Browse </a>
6, display the database table records, and do links to HTML pages: showit.asp
<!--#include file= "conn.asp"-->
<!--#include file= "lib.asp"-->
<%
Set rs = Server.CreateObject ("ADODB.") Recordset ")
sql = "SELECT * from C_news ORDER BY c_id Desc"
Rs. Open sql,conn,1,1
%>

<%
If Rs. EOF and Rs. BOF Then
Response.Write ("No article yet, <a href=add.html> add </a>")
Else
Do Until Rs. Eof
%>
<table width= "758" border= "0" align= "center" cellpadding= "3" cellspacing= "1" bgcolor= "#000000" >
<tr>
&LT;TD width= "159" align= "right" bordercolor= "#CCCCCC" bgcolor= "#CCCCCC" ><%=rs ("C_time")%></td>
&LT;TD width= "591" bordercolor= "#f3f3f3" bgcolor= "#f3f3f3" ><a href=<%=rs ("C_filepath")%> target= "A_" Blank "><%=rs (" C_title ")%></a></td>
</tr>
<tr>
&LT;TD valign= "Top" align= "right" bordercolor= "#ececec" bgcolor= "#ececec" >[<a href=del.asp?c_id=<%=rs ("C_ ID ")%>>dell</a>][<a href=change.asp?c_id=<%=rs (" c_id ")%>>edit</a>][<a href=" Add.html ">Add</a>]</td>
&LT;TD valign= "Top" bordercolor= "#FFFFFF" bgcolor= "#FFFFFF" ><%=htmlencode (RS ("C_content"))%></td>
</tr>
</table><br>
<%
Rs. MoveNext
Loop
End If
%>

<%
Rs.close
Set rs = Nothing
Conn.close
Set conn=nothing
%>
7, modify the Data content page change.asp

Modify the content of the data, but also need to modify the corresponding HTML page update. The modification is actually to regenerate the file, and the file name is the same as before, similar to file overwrite.
<!--#include file= "conn.asp"-->
<!--#include file= "lib.asp"-->

<%id=request.querystring ("c_id")%>

<%
If Request.Form ("submit") = "Change" Then
C_title=request.form ("C_title")
C_content=request.form ("C_content")
C_id=request.form ("c_id")
C_filepath=request.form ("C_filepath")

Set rs = Server.CreateObject ("ADODB.") Recordset ")
sql = "SELECT * from C_news where c_id=" &c_id
Rs. Open sql,conn,3,2
RS ("C_title") =c_title
RS ("C_content") =c_content
RS ("C_time") =now ()
Rs.update
Rs.close
Set rs = Nothing
%>

<% ' Open the template code and turn the special code into accepted values
sql1= "Select m_id,m_html from C_moban where m_id=1"
Set Rs1=server.createobject ("Adodb.recordset")
Rs1.open sql1,conn,1,1
Mb_code=rs1 ("m_html")
Rs1.close
Set rs1=nothing
Conn.close
Set conn=nothing
C_title=htmlencode (C_title)
C_content=htmlencode (c_content)
Mb_code=replace (Mb_code, "$cntop {Logcontent}quot;,now ()")
Mb_code=replace (Mb_code, "$cnleft {logcontent}quot;,c_title)
Mb_code=replace (Mb_code, "$cnright {logcontent}quot;,c_content)
%>

<% ' Generate HTML page
Set FSO = Server.CreateObject ("Scripting.FileSystemObject")
Set fout = fso. CreateTextFile (Server.MapPath (C_filepath))
Fout. WriteLine Mb_code
Fout.close
%>
<%response.redirect ("showit.asp")%>
<%end if%>

<%
If id<> "" Then
Set rs = Server.CreateObject ("ADODB.") Recordset ")
Sql= "SELECT * from C_news where c_id=" &id
Rs. Open sql,conn,1,1
C_id=rs ("c_id")
C_filepath=rs ("C_filepath")
C_title=rs ("C_title")
C_content=rs ("C_content")
End If
%>

<form action= "change.asp" method= "POST" >
Title:<input type= "text" name= "C_title" value=<%=c_title%>><br>
Content:<br>
<textarea name= "C_content" rows= "8" cols= "><%=c_content%></textarea><br>"
<input type= "Submit" value= "Change" name= "Submit" >
<input type= "reset" value= "reset" >
<input name= "c_id" type= "hidden" value= "<%=id%>" >
<input name= "C_filepath" type= "hidden" value= "<%=c_filepath%>" >
</form>
8, delete the record page del.asp

Same! Delete, in addition to deleting records in a database table, its corresponding HTML page also needs to be deleted. The code is as follows:
<!--#include file= "conn.asp"-->

<%
c_id = Request.QueryString ("c_id")
sql = "SELECT * from C_news where c_id=" &c_id
Set rs = Server.CreateObject ("ADODB.") Recordset ")
Rs. Open sql,conn,2,3

Filepath=rs ("C_filepath")
Set fso = CreateObject ("Scripting.FileSystemObject")
Fso. DeleteFile (Server.MapPath (filepath))
Set FSO = Nothing

Rs.delete
Rs.close
Set rs = Nothing
Conn.close
Set conn=nothing
%>

<%response.redirect ("showit.asp")%>
   Iv. Other functions

Template Management page:

Will not always open the database table to add or modify the template code, so, the management Code of the page program can not be less, their own tinkering should be very simple. Of course, the administrator's login authentication program is not in the book, and if the design of more than one template, then in the publication of information should be added template Select the Radio box, also in the implementation of the conversion of HTML, the different m_id of SQL selection.

In any case, first of all, these techniques to debug their own feelings. A lot of operation, I believe that "reading the thousand, its meaning from the see."

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.