菜头粿

标题: ASP技巧之简单模板实现 [打印本页]

作者: nhstbq    时间: 2009-8-29 12:14
标题: ASP技巧之简单模板实现(凤城社区)



  
   
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看看~~~
说明:上面显示的是代码内容。您可以先检查过代码没问题,或修改之后再运行.











欢迎光临 菜头粿 (https://cn.521000.cn/) Powered by Discuz! X3