JSON在模板中的应用~

三 5th, 2009

现在很多成熟的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 fto=nothing
	End Sub

呵呵,很简单吧~

标签:
  1. TryLife
    三 6th, 200903:37

    呵呵 做个沙发吧

  2. 茶叶
    六 21st, 200913:59

    非常好啊