关于生成HTML文件的新闻系统

来源:互联网 发布:我要说汉语阅读优化 编辑:程序博客网 时间:2024/06/15 13:40
<script type="text/javascript"><!--google_ad_client = "pub-8337741430802389";google_ad_width = 300;google_ad_height = 250;google_ad_format = "300x250_as";google_ad_type = "text_image";//2007-03-01: VBgoogle_ad_channel = "4923266371";google_color_border = "FFFFFF";google_color_bg = "FFFFFF";google_color_link = "0000FF";google_color_text = "000000";google_color_url = "008000";//--></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>
一般的传递ID值的新闻系统见得比较多,制作起来也不是很复杂。 
但是我们在新浪或是其它的门户类网站看到到的新闻不是用ID传递的,而是一个HTML或是Shtml文件,难道手工加上去的吗?当然不是了,其实这用到的是ASP中的FileSystemObject对象的方法。 
  接着往下说说如何实现这一功能。 
  首先考虑文件名的格式及文件的存放路径。 
  路径可以这样假设: 
  存放的路径:如果把所有的HTML文件或是所有的SHTML文件都放在同一目录下,当同时访问的人增多以后,势必会影响服务器正常的运行,所以明智的方法就是把这些文件按加入的日期进行分类。 
  文件名的格式: 
  为了避免出现重名的文件,一般的命名规则是当天的日期+时间+随机数+后缀名。 
function makefilename() 
fname = now() 
fname = replace(fname,'-','') 
fname = replace(fname,' ','') 
fname = replace(fname,':','') 
fname = replace(fname,'PM','') 
fname = replace(fname,'AM','') 
fname = replace(fname,'上午','') 
fname = replace(fname,'下午','') 
randomize 
ranNum=int(90000*rnd)+10000 '生成随机数 
fname = fname&fanNum 
makefilename=fname & '.shtml' 
 end function   这段函数就可以生成所需要的文件名及生成的文件的后缀名,当前的后缀名为.shtml,您也可以改成HTML或是ASP。 
  刚提到的是文件的命名,现在再来说一下文件的存放路径! 
  前面提到了,每个文件按加入的日期进行分类,那如何确定路径呢?同样用FSO生成。 
  用FSO中的folderExists方面来判断目录是否存在,如果存在就不需要重建立了,只需将HTML写入该目录下(这是后话了)。如果folderExists方法取得的值为false则表示目录不存在,就要用createfolder方法生成了。 
formPath=datepart('yyyy',now())&'_'&datepart('m',now())&'_'&datepart('d',now()) '建立目录名,生成的文件将按存放在此处 
set objfso = server.createobject('scripting.filesystemobject') 
if not objfso.folderexists(server.mappath(formPath)) then '如果该目录不存在 
objfso.createfolder(server.mappath(formPath)) '创建一个文件夹 
end if 
set objfso = nothing 
  目录就这样建立了。每次FSO生成文件时都进行判断一下,不过这样不知道会不会影响效率,大家可以有什么好的想法可以谈一谈。 
  先看一个模板: 
<html> 
<head> 
<title>柠檬树下----[title]</title> 
</head> 
<body> 
<script type="text/javascript"><!--google_ad_client = "pub-8337741430802389";google_ad_width = 300;google_ad_height = 250;google_ad_format = "300x250_as";google_ad_type = "text_image";//2007-03-01: VBgoogle_ad_channel = "4923266371";google_color_border = "FFFFFF";google_color_bg = "FFFFFF";google_color_link = "0000FF";google_color_text = "000000";google_color_url = "008000";//--></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>
[news_title] 
<br> 
作者:[author] 来源:[source] 加入日期:[add_time] 
<br> 
[content] 
</body> 
</html> 
  这是一个最简单的模板了,里面的用[]括起来的是什么东东呢?那就是即将要被替换的新闻标题、内容、作者等等。 
  先不管这个模板漂不漂亮,先写入数据表,在后面的用的哟! 
  下面将谈谈核心部分了,如何生成HTML的呢? 
  把前面的模板写入数据表,这里给出表的结构,当然是简单的一种 
Template表 
T_ID 'ID值,自动编号 
T_name '模板名称 
T_content '模板代码 
  在编辑器中写好新闻后,在处理页里的执行过程是这样的: 
  1、取得表单中的各变量值 
  2、从Template表中取得模板代码 
  3、将模板中的[]括起来的东东替换成相应的表单值,如[autor]就替换成表单中取得的作者名 
  4、FSO生成HTML文件 
  5、将新闻写入数据表(为什么要写入?以便以后的编辑) 
'这里查询模板 
strsql = 'select * from templet where T_name=''&templet_name&'' ' 
objrs.open strsql,conn,1,1 
content = objrs('content') 
objrs.close 
'开始替换,其中的title/strcontet都是从表单中取得的,前面的代码略去 
content = replace(content,'[title]',title) 
content = replace(content,'[content]',strcontent) 
content = replace(content,'[source]',source) 
content = replace(content,'[author]',author) 
content = replace(content,'[add_time]',now()) 

'然后将新闻写入数据表 
strsql = 'insert into news---------------' 
conn.execute strsql,intno '执行之 
if intno <> 0 then '如果执行结果不为0表示执行成功,则开始写入了 
set objfso = server.createobject('scripting.filesystemobject') 
formPath=datepart('yyyy',now())&'_'&datepart('m',now())&'_'&datepart('d',now()) '建立目录名,生成的文件将按存放在此处 
filename = server.mappath(formpath& '/' &makefilename) 
if objfso.folderexists(server.mappath(formPath)) then '如果该目录存在 
Set file = objfso.CreateTextFile(formpath&'/'&fname) 
原创粉丝点击