这里,当然,这个标签对项目是有意义的,表示控制全局的模块节点,如果在模板页中出现节点加载的就是对应名称为 format 的模块数据:
[%TITLE%]
这里我把这个模板节点定义成 加载控制全局的HTML源了,再看这个标签topdata:
{%TOP_MESS%}<%=show_top_meun()%>
如果这个节点被加载,生成的文件里面会将topdata节点替换成如上的HTML文档模板,这就是模块
这里的模块也可以是XML文档,重复加载模块,也可以是终数据.
当这些还有模块节点的XML模板被加载以后,就被程序识别,对应的加载成HTML二级模块,然后提出我们的"仿XML 数据"标记中的数据,正则替换对应的节点,生成文件,这里的"仿XML数据"是这种方式的数据:
{%TITLE%}<%=cjjer_hometitle%>{%/TITLE%}
{%STYLE%}default{%/STYLE%}
{%site_top%}<%=get_cache(0)%>{%/site_top%}
{%format_two%}
{%/format_two%}
{%site_footer%}<%Call cc_footer()%>{%/site_footer%}
这里,您可能马上理解了我说的"仿XML数据"了,这种加载数据的方式也是XML分析节点,然后直接正则替换,当然可以include文件的(ASP,PHP).
好了,现在您想必概念已经很清楚了,(不清楚的话重新看上面的话,或看如下的例子)
我就举个举个简单的例子说明一下(format_index.XML):
{%site_top%}
{%home_bigFlash%}
{%format_two%}
{%site_footer%}
模块:
//format,就是最上面的那个,不列举了
//home_bigFlash
//format_two
(&format_two&)
//site_footer
差不多应该加载的模板和模块就这点吧(都是可以重用的.)
现在是程序处理:
<%
'根据输入判断输出是名称还是数据
Function geturlXML(outfile,mode)
geturlXML=False
If outfile="" Then Exit Function
Dim tab
Select Case killint(mode,0,0,2)
Case 0:tab="szd_tpl"
Case 1:tab="szd_content"
End Select
Set rs=conn.execute("select ["&tab&"] FROM szd_ASP where [szd_link]='"&outfile&"'")
If rs.eof And rs.bof Then
Exit Function
else
geturlXML=rs(tab)
End If
End Function
'提取include文件到原始的数据项中
Function getincludefile(x)
Dim regXML,HTML
HTML=""
Set regXML=new regexp
regXML.ignorecase=True
regXML.global=True
regXML.pattern="()"
Dim Matches,Match
Set Matches = regXML.Execute(x)
For Each Match in Matches
HTML=HTML&match.value
Next
getincludefile=HTML
End Function
'获取模板项,先判断输出,返回值是 HTML 形式的模板,参数是模板的名称
Function gettpl(url)
Dim mudule_file,XML_file,ASP_file
XML_file=url
ASP_file=gettpldata(XML_file,0)'由模板名称获取模板的内容
Dim loadXML,parseXML,nodes,node
Set loadXML = Server.CreateObject("MSXML2.DOMDocument")
loadXML.async=false
parseXML=server.mappath("../"&gettpldata(XML_file,10)&XML_file)'装载本地的XML文档,先到数据库取到路径
loadXML.load parseXML
Set nodes = loadXML.selectNodes("//*")
for each node in nodes
ASP_file=insertASPinHTML(ASP_file,gettpldata(node.nodename,1),node.nodename)'加载模块到模板,输出HTML模板
Next
gettpl=ASP_file
End function
'匹配模板中的数据项
'把data中的数据传入HTML文件当中去
Function tpltodata(HTML,data)
Dim regXML
Set regXML=new regexp
regXML.ignorecase=True
regXML.global=True
regXML.pattern="({%)([a-zA-Z][A-Za-z0-9_]{2,60})(%})(.[^\[]*)({%/)\2(%})"
Dim Matches,Match
Set Matches = regXML.Execute(data)
For Each Match in Matches
HTML=Replace(HTML,"{%"®XML.Replace(match.value,"$2")&"%}",regXML.Replace(match.value,"$4"))
Next
tpltodata=HTML
End Function
'geturlXML 传入参数模板名称,返回模板的内容,失败的时候返回 null
Function geturlXML(outfile)
geturlXML=Null
If outfile="" Then Exit Function
Set rs=conn.execute("select [szd_tpl] FROM szd_ASP where [szd_link]='"&outfile&"'")
If rs.eof And rs.bof Then
Exit Function
else
geturlXML=rs("szd_tpl")
End If
End Function
public Function gettpldata(url,mode)
'Set rs=server.CreateObject("server.adodbrecordset")
If Len(url)<1 Then Exit Function
Dim szd_keytpl
Select Case mode
Case 0:Set rs=conn.execute("select [szd_content] from szd_tpl where [szd_link]='"&url&"'")
Case 10:Set rs=conn.execute("select [szd_url] from szd_tpl where [szd_link]='"&url&"'")
Case 1:Set rs=conn.execute("select [szd_content] from szd_keytpl where [szd_key]='"&url&"'")
End Select
If Not(rs.eof And rs.bof) Then
szd_keytpl=rs(0)
Else
szd_keytpl="
{%nodefined%}
"
End If
gettpldata=szd_keytpl
End Function
'加载标记的模块到 XML 格式的模板中,输出HTML格式的模板,ASP:XML格式模板,HTML:导入的模块,XMLs:当前标记
Function insertASPinHTML(ASP,HTML,XMLs)
Dim regXML,temp,temps,tempASP,tempASP_e
Set regXML=new regexp
regXML.ignorecase=True
regXML.global=True
regXML.pattern="(\<"&XMLs&"\>)(.[^\[]*)(\<\/"&XMLs&"\>)"
temp=regXML.Replace(ASP,"$2")
Dim Matches,Match
Set Matches = regXML.Execute(ASP)
For Each Match in Matches
HTML=Replace(HTML,"(&"&XMLs&"&)",regXML.Replace(match.value,"$2"))
ASP=Replace(ASP,Match.Value,HTML)
next
temps=Replace(HTML,"(&"&XMLs&"&)",temp)
insertASPinHTML=ASP
End Function
Function update_ASPfile(id)
Dim update_ASPfile_return
update_ASPfile_return=False
id=killint(id,0,0,14)
If id=0 Then Exit Function
Set rs=conn.execute("select [szd_level],[szd_content],[szd_tpl],[szd_link] from szd_ASP where id="&id&"")
If rs.eof And rs.bof Then Exit Function
Dim tpl_level,tpl_content,tpl_tpl,HTML_content,ASP_file_now
tpl_level=rs(0)
tpl_content=rs(1)
tpl_tpl=rs(2)
ASP_file_now=rs(3)
If Len(tpl_tpl)<4 Or tpl_level=1 Or IsNull(tpl_tpl) Then
HTML_content=tpl_content
Else
HTML_content=gettpl(tpl_tpl)
If Not isnull(HTML_content) Then
HTML_content=getincludefile(tpl_content)&tpltodata(HTML_content,tpl_content)
'response.write("
"&server.HTMLencode(getincludefile(tpl_content))&"
")
Else
HTML_content=tpl_content
'response.write("
"&server.HTMLencode(HTML_content)&"
")
End If
End If
If writeto("../",ASP_file_now,HTML_content,2) Then update_ASPfile_return=True
update_ASPfile=update_ASPfile_return
End Function
%>
生成文件:
运行代码框
[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]
这里,我的模块用了div,是利于样式表的使用.
主要思路如图:

其他的就不再说了,不知道说明白了.