| July 2010 万年历 | | Su | M | Tu | W | Th | F | Sa | | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | | < MONTH > < YEAR > | |
|
|
|
|
|
|
| Information |
Name:戴一波(cnbruce) Location:扬州@中国 MSN:cnbruce#msn.com Total Diary:1095 Reviews:7290 Members:49573 |
|
|
|
|
| Hot link |
|
+ Dreamweaver教程
+ 扬州分类信息网
+ 音乐前线播报
+ 泰兴人才网
+ 毛绒玩具批发
|
|
|
|
|
|
| 荐读:ASP,Web标准,PHP,用户体验,AJAX,《DW》书,DW2ASP,JSP,C#.Net
|
|
本日志标题:ASP应用之模板采用   [2004-5-29] 
|
初学ASP,程序是能勉强写出来了,但若每进行一次网站页面的改版,所有的源程序都将进行一次移植手术。为此所耗费的人力精力不计其数,甚至一不小心得不偿失、前功尽弃。
所以,梦想着那么大段的程序代码变成几个简单的字符代替,这样只要设计好页面把该功能插入就OK了。其实这也简单,只需将实现该功能的程序代码做成子程序,然后主页调用就可以了。
很多时候,在博客中国,你会选择到很多的模板,甚至有可能自己来设计;或者采用猪飞飞BLOG的各大站长都将其站点改得不近相同……这些,我们都归功于ASP采用模板的功能。
那下面偶就借花献佛,将模板拿来分析,以馈各位朋友。
首先,模板需要在线修改,则应采用数据库保存模板代码
所谓的模板,就是设计完工的标准的HTML代码,其中需要由程序实现的功能部分将采用特殊字符串代替。然,这些特殊字符串需要在显示的时候被编译为对应的功能。
1,设计数据库testmb.mdb 新建表moban:字段m_id(自动编号,主关键字);字段m_html(备注类型)
2,假设第一模板内容代码
将下列代码拷贝到m_html字段中
<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>
|
注意$cntop$、$cnleft$、$cnright$,它们将要实现某些具体的程序功能
3,建立数据库连接文件conn.asp
<% set conn= Server.CreateObject("ADODB.Connection") connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("testmb.mdb") conn.Open connstr %>
|
4,建立特殊字符串转换所需要的库文件lib.asp
该文件的主要作用是将实现某些功能的ASP程序做成字程序,以方便调用。
<% dim topcode sub cntop() topcode="现在时间是:" 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&i&i&i&i&i&">" next end sub %>
|
5,最后,调用数据库中的模板代码,将特殊字符串转换。
<!--#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 %>
|
该页主要作用是将模板代码进行显示,并将其中的特殊代码转变为相对应子程序功能。
至此,ASP的模板功能基本完成,剩下的就是:建立具备编辑模板功能的程序页面,将库文件改变为自己所需要程序功能……
在百度搜索完整的ASP应用之模板采用内容,或者用Google搜索相关的更多内容
|
About Comments |
 | | By [bts] at 2004-5-31 0:19:05 |
         | | By [909] at 2004-8-9 13:32:58 |
| 强~~ | | By [海春轩] at 2004-8-12 10:46:17 |
               | | By [zhk168] at 2004-8-21 14:23:15 |
  | | By [呵呵] at 2004-12-2 9:17:28 |
                | | By [00] at 2005-5-11 18:49:02 |
| 强人 | | By [1234] at 2005-8-13 22:01:49 |
  | | By [567] at 2005-12-14 14:07:51 |
              | | By [hdfhs] at 2006-1-5 14:50:53 |
                  | | By [AAAA] at 2006-9-10 0:31:36 |
|
|