存档
帮朋友写一个图片程序,写了快三个月了,还没写好,也不知道天天忙什么,在此检讨一下。今天朋友又要了,555555555555555~
现在要加油了。前几天写到了模板导入导出功能(请参考我的文章:JSON在模板中的应用~http://www.miaoqiyuan.cn/p/json-template/),今天来说下关于ASP程序调用模板的一些思考。实际程序的模板方面功能没有很复杂,根据朋友的要求,把模板标签分为全局标签,局部标签。包含局部标签的定义为复杂标签,反之不包含的为简单标签,初步设定模板的简单标签如下:
{csims:sitename}
{csims:keyword}
{csims:description}
{csims:webpath}
{csims:copyright}
复杂标签格式设定为{csims:标签标识,标签设定->输出格式},当然扩展性我认为还是不错的,举几个例子来说
{csims:menu->|<a href="$herf$">$name$</a>}
{csims:page,12-><a href="{csims:webpath}$pageurl$">$pageid$</a> }
其中$xxx$是局部标签,他的定义需要根据各个复杂标签的标识符和数据库中的相关字段来定义。
理论是有了,下面说下模板的使用方法,该系统需要实用ASP来当服务器脚本,我写得一个例子如下:
< %@codepage=65001%>
<!–#include file="../config/config.asp"–>
<!–#include file="../config/conn.asp"–>
< %
set rs=conn.execute("select index from template where use=true")
if not rs.eof then
index_html=rs(0)
else
rs.close
set rs=nothing
set rs=conn.execute("select index from template")
if not rs.eof then
index_html=rs(0)
else
response.write "无法加载模板"
end if
end if
index_html=replace(index_html,"{csims:sitename}","网站名称")
index_html=replace(index_html,"{csims:keyword}","页面关键字")
index_html=replace(index_html,"{csims:description}","页面描述")
index_html=replace(index_html,"{csims:webpath}",webpath)
index_html=replace(index_html,"{csims:copyright}","版权所有")
set csimsRegExp=new regexp
csimsRegExp.ignorecase=true
csimsRegExp.global=true
csimsRegExp.Pattern = "{csims:([^}]*)\-\>([^}]*)}"
set cslists=csimsRegExp.execute(index_html)
for each pptext in cslists
csims_HTML="<p>标签:"&pptext.submatches(0)&"<br />"&vbCrlf
csims_HTML=csims_HTML+"格式:"&pptext.submatches(1)&"</p>"&vbCrlf&vbCrlf
index_html=replace(index_html,pptext,csims_HTML)
next
response.write index_html
‘zz="{csims:menu-><a href="$herf$">$name$</a>}"
%>
上面的程序比较粗糙,还没有写出解释局部变量的代码,但是思路是正确的,在次基础上继续修改应该是可行的。
好久没有写博客了,博客IP还剩四五十,哎。懒了~确实该反省下了。。。。。
现在很多成熟的CMS(如织梦,无忧。。。)都使用的模板的功能。模板的作者信息很多使用XML来描述,这种方法对于我等菜鸟可能有一定的难度,今天,我给大家提供另一种方法,用JSON方法,优点如下:
1.便于输出
2.格式简单直观
3.在Js中可直接实用
下面是我写个一个描述信息info.json.txt。
var $CSIMS_Tmplate={
template_name:’CSIMS默认模板’,
template_type:’html’,
template_generator:’猫七工作室(CatSeven Studio)’,
template_csimsver:’1.0′,
template_home:’http://www.catseven.cn/?goto=csims’
}
使用的时候可以直接使用如下代码调入数据:
<script type="text/javascript" src="{path}/info.json.txt"></script>
通过JS加载这些数据创建一个表单,并自动提交:
function _CreateForms(){
if(typeof($CSIMS_Tmplate)=="object"){
document.writeln(’<form action="cgi?f=save_template" method="post" id="autosend">’);
_Input("autosubmit","yes");
_Input("name",$CSIMS_Tmplate.template_name);
_Input("type",$CSIMS_Tmplate.template_type);
_Input("generator",$CSIMS_Tmplate.template_generator);
_Input("csimsver",$CSIMS_Tmplate.template_csimsver);
_Input("home",$CSIMS_Tmplate.template_home);
document.writeln(’</form>’);
document.getElementById("autosend").submit();
}else{
_Readerr();
}
}
function _Input(n,v){
document.writeln(’<input type="hidden" name="’+n+’" value="’+v+’" />’);
}
function _Readerr(){
alert(’读取配置文件(info.json.txt)出错’);
location.href=’err?err_id=template|1′;
return false;
}
window.onload=_CreateForms;
通过表单提交相关信息,然后通过程序保存模板。这种方法就简单了,而且效率也不错。
下面谈下输出json的方法,各种语言不同,以ASP为例:
Sub TemplateOutput()
on error resume next
set rs=conn.execute("select * from template where use=true")
if not rs.eof then
tname=rs("name")
ttype=rs("type")
tgenerator=rs("generator")
tcsimsver=rs("csimsver")
thome=rs("home")
tindex=rs("index")
tclass=rs("class")
tpage=rs("page")
else
set rs=nothing
set rs=conn.execute("select * from template")
if not rs.eof then
tname=rs("name")
ttype=rs("type")
tgenerator=rs("generator")
tcsimsver=rs("csimsver")
thome=rs("home")
tindex=rs("index")
tclass=rs("class")
tpage=rs("page")
else
selTemplate "在数据中没有发现模板"
response.end
end if
end if
set rs=nothing
tjson="var $CSIMS_Tmplate={"&vbCrlf
tjson=tjson&" template_name:’"&tname&"’,"&vbCrlf
tjson=tjson&" template_type:’"&ttype&"’,"&vbCrlf
tjson=tjson&" template_generator:’"&tgenerator&"’,"&vbCrlf
tjson=tjson&" template_csimsver:’"&tcsimsver&"’,"&vbCrlf
tjson=tjson&" template_home:’"&thome&"’"&vbCrlf
tjson=tjson&"}"&vbCrlf
set fso=server.createobject("scripting.filesystemobject")
Writetxt server.mappath(csimspath&"template/class.html"),fso,tclass
Writetxt server.mappath(csimspath&"template/index.html"),fso,tindex
Writetxt server.mappath(csimspath&"template/page.html"),fso,tpage
Writetxt server.mappath(csimspath&"template/info.json.txt"),fso,tjson
selTemplate "导出成功"
End Sub
Sub Writetxt(file,fso,txt)
if fso.fileexists(file) then
fso.getfile(file).delete
end if
set fto=fso.opentextfile(file,2,1,-1)
fto.write txt
fto.close
set [...]