Examples of teaching how to automatically generate HTML files using an ultra-complete ASP program

Source: Internet
Author: User
Tags newsfile
, Design the database testmb. MDB
Create a table moban: Field m_id (automatic number, primary keyword); field m_html (Remarks type)

2. Assume that the content of the first template isCode

Copy the following code to the m_html field.
The following is a code snippet:
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> testmb </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 $ </TD>
</Tr>
<Tr valign = "TOP">
<TD width = "25%" bgcolor = "# e5e5e5"> $ cnleft $ </TD>
<TD width = "74%" bgcolor = "# f3f3f3"> $ cnright $ </TD>
</Tr>
</Table>
</Body>
</Html>
Note $ cntop $, $ cnleft $, and $ cnright $. They will implement some specificProgramFunction

3. Create the database connection file conn. asp
The following is a code snippet:
<%
Set conn = server. Createobject ("ADODB. Connection ")
Connstr = "provider = Microsoft. Jet. oledb.4.0; Data Source =" & server. mappath ("testmb. mdb ")
Conn. Open connstr
%>
4. Create the library file Lib. asp required for special String Conversion

The main function of this file is to make ASP programs that implement certain functions into word programs for convenient calls.
The following is a code snippet:
<%
Dim topcode
Sub cntop ()
Topcode = "the current time is :"
Topcode = topcode & now ()
End sub

Dim leftcode, I
Sub cnleft ()
For I = 1 to 5
Leftcode = leftcode & "<p> cnbruce.com"
Next
End sub

Dim rightcode
Sub cnright ()
For I = 1 to 9
Rightcode = rightcode & "<HR color =" & I & ">"
Next
End sub
%>
5. Finally, call the template code in the database to convert special strings.
The following is a code snippet:
<! -- # Include file = "conn. asp" -->
<! -- # Include file = "Lib. asp" -->
<%
SQL = "select * From moban where m_id = 1"
Set rs = server. Createobject ("ADODB. recordset ")
Rs. Open SQL, Conn, 1, 1
Mb_code = RS ("m_html ")
Rs. Close
Set rs = nothing

Cntop ()
Mb_code = Replace (mb_code, "$ cntop $", topcode)
Cnleft ()
Mb_code = Replace (mb_code, "$ cnleft $", leftcode)
Cnright ()
Mb_code = Replace (mb_code, "$ cnright $", rightcode)

Response. Write mb_code
%>
This page is mainly used to display the template code and convert the special code into the corresponding subroutine function.

At this point, the ASP template function is basically complete, and the rest is: create a program page with the template editing function, change the library file to your desired program function ......

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 ...... Usually you may see some addresses such as http://www.xxx.com/a/2004-5-20/200405201111.html; you can analyze the folder that should be created for the current date. 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:

On Error resume next

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

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 :)

<%
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.

<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 $ </TD>
</Tr>
<Tr valign = "TOP">
<TD width = "25%" bgcolor = "# e5e5e5"> $ cnleft $ </TD>
<TD width = "74%" bgcolor = "# f3f3f3"> $ cnright $ </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, saveArticleTitle
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

<%
''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, ">", "> ")
Fstring = Replace (fstring, "<", "<")
Fstring = Replace (fstring, CHR (32 ),"")
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
<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.

<% ''Fault tolerance Processing
On Error resume next
%>

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

<% ''Accept pass 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 $", now ())
Mb_code = Replace (mb_code, "$ cnleft $", c_title)
Mb_code = Replace (mb_code, "$ cnright $", c_content)
%>

<% ''To 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

<! -- # 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 $", now ())
Mb_code = Replace (mb_code, "$ cnleft $", c_title)
Mb_code = Replace (mb_code, "$ cnright $", c_content)
%>

<% ''To 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>

 

7. Modify the data content page Chang. 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.

<! -- # 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 $", now ())
Mb_code = Replace (mb_code, "$ cnleft $", c_title)
Mb_code = Replace (mb_code, "$ cnright $", c_content)
%>

<% ''To 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:

<! -- # 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.

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.