欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

浅谈 ASP 模板技术之参数传递

程序员文章站 2022-07-02 18:23:11
在内容系统开发中,涉及内容和形式分离的过程,也就是根据用户自定义页面模板然后替换成相关内容的过程。这和外面很多整站的内容管理系统,有本质上的区别。有不少内容管理系统,多少人...

在内容系统开发中,涉及内容和形式分离的过程,也就是根据用户自定义页面模板然后替换成相关内容的过程。这和外面很多整站的内容管理系统,有本质上的区别。有不少内容管理系统,多少人用,都是一个样子,因为页面无法自定义,不懂编程的用户无法修改。象那种,只填几个参数就出来的网站,我估计是没有什么前途的。因为人人都是一个样子,人人都是会填那些参数的。

举个例子,你查看一下以下几个站点,你会认为他们是一套程序吗?






如果我告诉你,他们都是一个程序,只是由相关的站长,设计不同的模板得到的页面显示,你就会发现,这个系统的优良性。

当然由于这套系统的高端性,目前普通用户无法使用,于是我开发了我自己的内容管理系统 kiss 内容管理系统。

而要给用户一个模板系统,首先,就是要有一个简单易懂的标记系统。大家看看下面的代码,看是否容易理解:
<tag:loop channelid="1" pagesize="10" title="20" type="new" column="1">

略有html经验的人,就知道,这是一个模板标记里的循环标记,因为这是最常用的,你看我们网站的首页,列出10条文档也就只需要写一个这样的标记就完成了,这是不是让不明白编程的人,也很容易做出自己设计的页面出来呢?

参数说明:
channelid 为一个栏目的在数据库中的id
pagesize 为列举多少个文档
title 为标题的长度
type 为列表列型,这里的”new”我们设定为最新的文档
column 为显示几列

以上介绍是给不会编程,或者对不了解内容系统的人做个普及,并且给我的内容管理系统打个广告,而且我想说的是,蓝色理想站点用的内容管理系统模板模块,要比我的强大很多。

下面轮到程序员了,其它人可以不用往下看。
那么怎么把它们的值读出来呢?
下面这个函数是最后的,用来解析所有模板的内容

复制代码 代码如下:

'【功能】自定义模板标签
function processcustomtags(byval scontent)
         dim objregex, match, matches
      '建立正则表达式
         set objregex = new regexp
      '查找内容
         objregex.pattern = "<tag:.*/>"
      '忽略大小写
         objregex.ignorecase = true
      '全局查找
         objregex.global = true
      'run the search against the content string we've been passed
         set matches = objregex.execute(scontent)
      '循环已发现的匹配
         for each match in matches
         'replace each match with the appropriate html from our parsetag function
         scontent = replace(scontent, match.value, parsetag(match.value))
         next
      '消毁对象
         set matches = nothing
         set objregex = nothing
      '返回值
         processcustomtags = scontent
end function

  在上面的代码中,用到了正则表达式,如果你对它还不是很了解,请参阅相关资料,这里就不详细介绍了。

那么怎么取出参数值呢,也是一个函数:代码拷贝框

复制代码 代码如下:

'【功能】取得模板标签的参数名
'如:<tag:loop channelid="1" pagesize="10" title="20" type="new" column="1">
function getattribute(byval strattribute, byval strtag)
      dim objregex, matches
      '建立正则表达式
         set objregex = new regexp
      '查找内容 (the attribute name followed by double quotes etc) 
         objregex.pattern = lcase(strattribute) & "=""[0-9a-za-z]*"""
      '忽略大小写
         objregex.ignorecase = true
      '全局查找
         objregex.global = true
      '执行搜索
         set matches = objregex.execute(strtag)
      '如有匹配的则返回值, 不然返回空值
         if matches.count > 0 then
              getattribute = split(matches(0).value,"""")(1)
         else
              getattribute = ""
         end if
      '消毁对象
         set matches = nothing
         set objregex = nothing
end function

ok好了,那怎么解析像上面<tagloop:>内容呢?
下面就是一个函数:

复制代码 代码如下:

'【功能】解析并替换相应的模板标签内容
function parsetag(byval strtag)
      dim arrresult, classname, arrattributes, stemp, i, objclass
      '如果标签是空的则退出函数
         if len(strtag) = 0 then exit function
      'split the match on the colon character (:)
         arrresult = split(strtag, ":")
      'split the second item of the resulting array on the space character, to
         'retrieve the name of the class
         classname = split(arrresult(1), " ")(0)
         'use a select case statement to work out which class we're dealing with
         'and therefore which properties to populate etc
         select case ucase(classname)
         'it's a loop class, so instantiate one and get it's properties
         case "loop"
                     set objclass = new loop_class
                     loop.channelid= getattribute("channelid", strtag")
                     loop.pagesize= getattribute("pagesize", strtag")
                     loop.title = getattribute("title", strtag")
                     loop.type = getattribute("type", strtag")
                     parsetag = loop.column (getattribute("column", strtag"), true)
                     'destroy our class object
                     set objclass = nothing
         end select
end function


上面的loop是一个类,这里也不再详说了。因为好久没有说话了,不太习惯,呵呵。
  结论,通过上面的函数,你可以很快的编写相关的模板程序了。希望对你有帮助。