ASP generate static files

Source: Internet
Author: User
Tags newsfile

ASP2HTML WITH TEMPLET

I hope that you can see this title to see its functions:

1. with templet means that a preset template will be used for the generated page architecture. I have introduced this in a previous tutorial, I hope you will be familiar with ASP template before reading this tutorial. (Of course, don't look at it is no problem, this tutorial will also mention the essence of the part :) specific reference: http://www.cnbruce.com/blog/showlog.asp? Cat_id = 26 & log_id = 474

2. ASP2HTML. Don't let me talk about the advantages of converting ASP into HTML. Haha, the most important thing to know is that static HTML pages and dynamic pages have much lower server requirements. Similarly, static HTML search is much more likely than dynamic pages.

Now, the technical problems I need to solve are:
1. How to Implement the template technology? (See the previous article)
2. How to Implement 2HTML technology?
3. How to integrate the template technology with 2HTML technology?

I. Analysis of Technical Principles

1. For template technology, see www.cnbruce.com/blog/showlog.asp? Cat_id = 26 & log_id = 474

2. How can we implement 2HTML technology? How can I convert an ASP page to HTML? The FSO component can be used to create any file format.

So what is the overall running process?
A. Provide the Information Input page for information collection;
B. Save the database before receiving the information value, and then generate a file using FSO;
C. Technically complete the task and display the path of the newly created HTML file.

The implementation process of this technology has the following difficulties:

I. Is the file generated by FSO directly placed in a large folder or separately in a daily update subfolders? The statement may be inaccurate. I believe that over time, the number of files generated through FSO will increase, and management will become increasingly messy ...... Generally, you may see some URLs such as www.xxx.com/a/2004-5-20/200405201111.html. You can find that the folder of the current date is created. In this way, one day is the page content of a folder, so it is reasonable to view and manage it.

Ii. When I tried to create a folder using the above method, I found the second problem. It is no problem to create a folder named after the current date through FSO for the first time. When I have a new file to be generated, because it is the same program, it will create the same folder again. In this case, the FSO component will find that the path already exists ...... Case -_-! To continue the process, add the code in the first line:

Reference:

On Error Resume Next

Hey hey, you have achieved the effect of deceiving yourself and stealing your ears.

Of course, the rule is to determine whether a folder exists or not.

Reference:

<%
Set fso = Server. CreateObject ("Scripting. FileSystemObject ")
If (fso. FolderExists (Server. MapPath (folder) then
'Do not process the judgment if it exists.
Else
'Create a new folder if it does not exist.
Fso. CreateFolder (Server. MapPath (folder ))
End if
%>

Iii. How do I create a folder? This mainly refers to the generation of file names. Of course, you need to write a function by yourself. The function is to generate a file name :)

Reference:

<%
Function makefilename (fname)
Fname = fname' fname is a variable, and fname is a function parameter reference.
Fname = replace (fname ,"-","")
Fname = replace (fname ,"","")
Fname = replace (fname ,":","")
Fname = replace (fname, "PM ","")
Fname = replace (fname, "AM ","")
Fname = replace (fname, "Morning ","")
Fname = replace (fname, "Afternoon ","")
Makefilename = fname & ". html"
End function
%>

When referencing a function:
<% Fname = makefilename (now () %>

In fact, it is a file named after year, month, day, hour, minute, and second.

Iv. How can I view the generated file? Of course, you need to save the path of the generated file to the database and add it to the corresponding record set. Of course, this will be 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 value accepted from the form to complete the template function; generate HTML files for all the final replaced template codes. Note that the replacement should completely change the format of the input data or the Code supporting UBB.

2. design the database

Currently, two tables are required for Database Design: one for storing template data and the other for storing information.

1. Create a new database asp2html. mdb

2. design the new database table c_moban
Field m_id (automatic number, primary keyword); field m_html (Remarks type ).
Copy the following complete code to the m_html field.

Reference:

<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = hz">
<Title> Cnbruce. Com | ASP2HTML TEST </title>
</Head>
<Body leftmargin = "0" topmargin = "0">
<Table width = "100%" height = "100%" border = "0" cellpadding = "5" cellspacing = "2">
<Tr align = "right" bgcolor = "# CCCCCC">
<Td height = "20" colspan = "2"> $ cntop {LogContent} lt;/td>
</Tr>
<Tr valign = "top">
<Td width = "25%" bgcolor = "# e5e5e5"> $ cnleft {LogContent} lt;/td>
<Td width = "74%" bgcolor = "# f3f3f3"> $ cnright {LogContent} lt;/td>
</Tr>
</Table>
</Body>
</Html>

3. Design the new database table c_news

Field c_id: automatic number, primary keyword
Field c_title: text type. Save the article title.
Field c_content: remarks type. Save the article content.
Field c_filepath: text type, retain the path address of the generated file
Field c_time: date/time type; default value: Now ()

3. Page requirement Design

1. Create a folder to store HTML pages

Create a folder newsfile under the same directory of the file. The folder mainly stores the generated HTML page. Of course, the folder will be created by using a program to facilitate browsing and management.

2. Function page lib. asp

Reference:

<%
'Function for generating file names
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, "Morning ","")
Fname = replace (fname, "Afternoon ","")
Makefilename = fname & ". shtml"
End function

'Functions that keep the data format unchanged
Function HTMLEncode (fString)
FString = replace (fString, ">", "& gt ;")
FString = replace (fString, "<", "& lt ;")
FString = Replace (fString, CHR (32), "& nbsp ;")
FString = Replace (fString, CHR (13 ),"")
FString = Replace (fString, CHR (10) & CHR (10), "<br> ")
FString = Replace (fString, CHR (10), "<br> ")
HTMLEncode = fString
End function
%>

3. Database Connection page conn. asp
Complete the database string connection method
<%
Set conn = Server. CreateObject ("ADODB. Connection ")
Connstr = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" & Server. MapPath ("asp2html. mdb ")
Conn. Open connstr
%>
4. add.html
Actually, it's very simple: form. Note that action is redirected to addit. asp

Reference:

<Form action = "addit. asp" method = "post">
Title: <input type = "text" name = "c_title"> <br>
Content: <br>
<Textarea name = "c_content" rows = "8" cols = "30"> </textarea> <br>
<Input type = "submit" value = "Add">
<Input type = "reset" value = "Reset">
</Form>

5. addit. asp
First, process the accepted data and write the value to the database. Then, reference the template code, convert the special code to the accepted value, and generate an HTML page through FSO. Note that the path address of the generated file is saved to the database table.

Reference:

<% 'Fault tolerance handling
On Error Resume Next
%>

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

<% 'Accept transfer value
C_title = request. form ("c_title ")
C_content = request. form ("c_content ")
%>

<% ': Generate the HTML file name, create a folder, and specify the file path.
Fname = makefilename (now () 'makefilename is a custom function
Folder = "newsfile/" & date ()&"/"
Filepath = folder & fname
%>

<% 'Keep the accept value and path 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 convert the special code to the accept value.
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 an 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
%>

The article is successfully added. <a href = "showit. asp"> browse </a>

6. Display database table records and link to the HTML page: showit. asp

Reference:

<! -- # 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 ("there are no articles 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>
<Td width = "159" align = "right" bordercolor = "# CCCCCC" bgcolor = "# CCCCCC"> <% = rs ("c_time") %> </td>
<Td width = "591" bordercolor = "# f3f3f3" bgcolor = "# f3f3f3"> <a href = <% = rs ("c_filepath ") %> target = "a_blank"> <% = rs ("c_title") %> </a> </td>
</Tr>
<Tr>
<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>
<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 data content and update the corresponding HTML page. The modification is actually to regenerate the file, and the file name is the same as before, similar to the file overwrite.

Reference:

<! -- # 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 convert the special code to the accept value.
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 an 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 = "30"> <% = 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 database tables, the corresponding HTML page also needs to be deleted. The Code is as follows:

Reference:

<! -- # 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") %>

4. Other functions

Template Management page:

It will not always open the database table to add or modify the template code. Therefore, the page program for code management should not be less, and it should be very easy to do it by yourself. Of course, the previous administrator login authentication program will not be described in the book :) also, if multiple templates are designed, you should add a template to select a single region when posting information, similarly, different m_ids are selected for SQL during HTML conversion.

In any case, debug these technologies first. More operations, I believe that "reading for thousands of times, its own opinions ".

Debugging address: www.cnbruce.com/test/asp2html/showit.asp

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.