菜头粿

查看: 282|回复: 0
打印 上一主题 下一主题

ASP技巧之简单模板实现

[复制链接]
1#
跳转到指定楼层
发表于 2009-8-29 12:14:02 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式



  
   
ASP技巧之简单模板实现
以下内容含脚本,或可能导致页面不正常的代码
很多程序都采用模板,模板可以把程序和美工分离开,在不需要改变程序的前提下美化程序~~

如:DBLOG,DVBBS,雪人论坛等都是采用模板,使用户可以方便地美化程序~

以下就是简单的实现方法

首先建立数据库,建表为template 里面有两个字段id(数值型) 和 testtemp(备注型)
怎样连接数据这些就不再详细讲了

以下是读取模板的函数:

function loadtemplate(name)
'读模板把模板存在template变量中
  dim template,sql
  sql="select * from template where id=1"
  set temp = Server.createObject("ADODB.Recordset")
  temp.open Sql,conn,1,1
  template=temp(name)
  temp.close
  set temp=nothing
  loadtemplate=template
end function

假设testtemp字段里面有以下数据:

<html>
<head>
<title>$title$</title>
</head>
<body>
$nowtime$
</body>
</html>

建立页面test.asp以下是内容:

<!--#include file="conn.asp" -->
<%
function loadtemplate(name)
'读模板把模板存在template变量中
  dim template,sql
  sql="select * from template where id=1"
  set temp = Server.createObject("ADODB.Recordset")
  temp.open Sql,conn,1,1
  template=temp(name)
  temp.close
  set temp=nothing
  loadtemplate=template
end function
dim m_temp,title,nowtime
title="测试页面"
nowtime=now
m_temp=loadtemplate(testtemp)
m_temp=replace(m_temp,"$title$",title)
m_temp=replace(m_temp,"$nowtime$",nowtime)
response.write m_temp
response.end
conn.close
set conn=nothing
%>

运行test.asp看看~~~
说明:上面显示的是代码内容。您可以先检查过代码没问题,或修改之后再运行.






高级模式
B Color Image Link Quote Code Smilies

本版积分规则

手机版|Archiver| 菜头粿 声明:本站所发贴子内容仅代表网友观点,不代表本站立场,严禁一切与国家法律相抵触的内容。感谢关注!
粤icp备19073828

GMT+8, 2026-7-26 15:49 , Processed in 0.081911 second(s), 15 queries .

粤东.中国

© 2007-2026 czczcz.com

快速回复 返回顶部 返回列表