javascript

位置:IT落伍者 >> javascript >> 浏览文章

Javascript模板技术


发布日期:2021年07月04日
 
Javascript模板技术

/***Templateclassjs***/

function Template()

{

thisclassname=Template;

thisdebug=false;

thisfile=new HashMap();

thisroot=;

thisvarkeys=new HashMap();

thisvarvals=new HashMap();

thisunknowns=remove;

thishalt_on_error=yes;

thislast_error=;

thisfso=new ActiveXObject(ScriptingFileSystemObject);

thisset_root=_set_root;

thisset_unknowns=_set_unknowns;

thisget_var=_get_var;

thisset_file=_set_file;

thisset_var=_set_var;

thisset_block=_set_block;

thissubst=_subst;

thisparse=_parse;

thisp=_p;

thispparse=_pparse;

thisfinish=_finish;

thisloadfile=_loadfile;

thisis_dir=_is_dir;

thisfile_exists=_file_exists;

thisfilename=_filename;

thisvarname=_varname;

thishalt=_halt;

thishaltmsg=_haltmsg;

}

/**

* 设置模板文件根目录

* @param root

*/

function _set_root(root)

{

if(!thisis_dir(root))

{

thishalt(set_root:+root+ is not a directory);

}

thisroot=root;

}

/**

* 设定对未知模板变量的处理办法

* @param unknowns

*/

function _set_unknowns(unknowns)

{

thisunknowns=unknowns;

}

/**

* 设定模板文件

* @param handle

* @param filename

*/

function _set_file(handlefilename)

{

thisfileput(handlethisfilename(filename));

}

/**

* 设定模板变量

* @param varname

* @param value

*/

function _set_var(varnamevalue)

{

if(!ntainsKey(varname))

{

thisvarkeysput(varnamethisvarname(varname));

}

if(!ntainsKey(varname))

{

thisvarvalsput(varnamevalue);

}

else

{

thisvarvalsremove(varname);

thisvarvalsput(varnamevalue);

}

//alert(varname+==================+value);

}

/**

* 设定块变量

* @param parent

* @param handle

* @param name

*/

function _set_block(parenthandlename)

{

if(!thisloadfile(parent))

{

thishalt(subst:unable to load +parent);

}

if(name==)

{

name=handle;

}

var str=thisget_var(parent);

var re=new RegExp(<!\\s+BEGIN + handle + \\s+>([\\s\\S]*)<!\\s+END + handle + \\s+>);

//Matcher m=pmatcher(str);

//var rs=mfind();

//var t=mgroup(mgroupCount());

//thisset_var(handlet);

var arr=reexec(str);

if(arr!=null)

thisset_var(handleRegExp$);

str=strreplace(re{+name+});

thisset_var(parentstr);

}

/**

* 进行变量替换

* @param handle

* @return

*/

function _subst(handle)

{

if(!thisloadfile(handle))

{

thishalt(subst:unable to load +handle);

}

var str=thisget_var(handle);

var keys=thisvarkeyskeySet();

//alert(keyslength);

for(var i=;i<keyslength;i++)

{

var key=keys[i];

var re=new RegExp(thisvarkeysget(key)g)

str=strreplace(rethisvarvalsget(key));

}

//alert(handle++++++++++++++++++++str);

return str;

}

/**

* 进行变量复制

* @param target

* @param handle

* @param append

*/

function _parse(targethandleappend)

{

var str=thissubst(handle);

if(append)

{

thisset_var(targetthisget_var(target)+str);

}

else

{

thisset_var(targetstr);

}

}

/**

* 返回替换后的文件

* @param varname

* @return

*/

function _p(varname)

{

return thisfinish(thisget_var(varname));

}

/**

* parse()和p()的合并

* @param target

* @param handle

* @param append

* @return

*/

function _pparse(targethandleappend)

{

thisparse(targethandleappend);

documentwriteln(thisp(target));

}

/**

* 加载模板文件

* @param handle

* @return

*/

function _loadfile(handle)

{

if(ntainsKey(handle) && thisvarvalsget(handle)!=null)

{

return true;

}

if(!ntainsKey(handle))

{

_halt(loadfile:+handle+ is not a valid handle);

return false;

}

var filename=thisfileget(handle);

if(!thisfile_exists(filename))

{

thishalt(loadfile:while loading +handle++filename+ does not exist);

return false;

}

try

{

var fr=thisfsoOpenTextFile(filename);

var s=frReadAll();

if(s==)

{

halt(loadfile:while loading +handle++filename+ is empty);

return false;

}

thisset_var(handles);

}

catch(e)

{

}

return true;

}

/**

* 获取变量

* @param varname

* @return

*/

function _get_var(varname)

{

if(ntainsKey(varname))

return thisvarvalsget(varname);

else

return ;

}

/**

* 判断目录

* @param path

* @return

*/

function _is_dir(path)

{

if(thisfsoFolderExists(path))

return true;

else

return false;

}

/**

* 判断文件

* @param filename

* @return

*/

function _file_exists(filename)

{

if(thisfsoFileExists(filename))

return true;

else

return false;

}

/**

* 文件名处理

* @param filename

* @return

*/

function _filename(filename)

{

if(!thisfile_exists(thisroot+filename))

{

thishalt(filename:file +filename+ does not exist);

}

return thisroot+filename;

}

/**

* 变量名处理

* @param varname

* @return

*/

function _varname(varname)

{

return {+varname+};

}

/**

* 完成字符串的处理

* @param str

* @return

*/

function _finish(str)

{

var re=new RegExp({[^ \\t\\r\\n\\}]+\\}g);

if(thisunknowns==remove)

{

str=strreplace(re);

}

else if(thisunknowns==comment)

{

str=strreplace(re<! Template Variable undefined >);

}

else

{

;

}

return str;

}

function _halt(msg)

{

thislast_error=msg;

if(thishalt_on_error!=no)

{

_haltmsg(msg);

}

if(thishalt_on_error==yes)

{

alert(Halted);

//Systemexit();

}

}

function _haltmsg(msg)

{

alert(Template Error:+msg);

}

/**

* HashMap构造函数

*/

function HashMap()

{

thislength = ;

thisprefix = hashmap_prefix__;

}

/**

* 向HashMap中添加键值对

*/

HashMapprototypeput = function (key value)

{

this[thisprefix + key] = value;

thislength ++;

}

/**

* 从HashMap中获取value值

*/

HashMapprototypeget = function(key)

{

return typeof this[thisprefix + key] == undefined

? null : this[thisprefix + key];

}

/**

* 从HashMap中获取所有key的集合以数组形式返回

*/

HashMapprototypekeySet = function()

{

var arrKeySet = new Array();

var index = ;

for(var strKey in this)

{

if(strKeysubstring(thisprefixlength) == thisprefix)

arrKeySet[index ++] = strKeysubstring(thisprefixlength);

}

return arrKeySetlength == ? null : arrKeySet;

}

/**

* 从HashMap中获取value的集合以数组形式返回

*/

HashMapprototypevalues = function()

{

var arrValues = new Array();

var index = ;

for(var strKey in this)

{

if(strKeysubstring(thisprefixlength) == thisprefix)

arrValues[index ++] = this[strKey];

}

return arrValueslength == ? null : arrValues;

}

/**

* 获取HashMap的value值数量

*/

HashMapprototypesize = function()

{

return thislength;

}

/**

* 删除指定的值

*/

HashMapprototyperemove = function(key)

{

delete this[thisprefix + key];

thislength ;

}

/**

* 清空HashMap

*/

HashMapprototypeclear = function()

{

for(var strKey in this)

{

if(strKeysubstring(thisprefixlength) == thisprefix)

delete this[strKey];

}

thislength = ;

}

/**

* 判断HashMap是否为空

*/

HashMapprototypeisEmpty = function()

{

return thislength == ;

}

/**

* 判断HashMap是否存在某个key

*/

HntainsKey = function(key)

{

for(var strKey in this)

{

if(strKey == thisprefix + key)

return true;

}

return false;

}

/**

* 判断HashMap是否存在某个value

*/

HntainsValue = function(value)

{

for(var strKey in this)

{

if(this[strKey] == value)

return true;

}

return false;

}

/**

* 把一个HashMap的值加入到另一个HashMap中参数必须是HashMap

*/

HashMapprototypeputAll = function(map)

{

if(map == null)

return;

if(nstructor != JHashMap)

return;

var arrKey = mapkeySet();

var arrValue = mapvalues();

for(var i in arrKey)

thisput(arrKey[i]arrValue[i]);

}

//toString

HashMapprototypetoString = function()

{

var str = ;

for(var strKey in this)

{

if(strKeysubstring(thisprefixlength) == thisprefix)

str += strKeysubstring(thisprefixlength)

+ : + this[strKey] + \r\n;

}

return str;

}

<!  >

<!DOCTYPE HTML PUBLIC //WC//DTD HTML Transitional//EN

>

<html>

<head>

<meta httpequiv=ContentType content=text/html; charset=gb>

<title>无标题文档</title>

</head>

<body>

<p>{HEAD}</p>

<p>{WELCOME}

</p>

<table width=% border= cellspacing= cellpadding=>

<! BEGIN BROWS >

<tr>

<! BEGIN BCOLS >

<td>{NUMBER}</td>

<! END BCOLS >

</tr>

<! END BROWS >

</table>

<p>{FOOT}</p>

</body>

</html>

<!  >

<table width=% border= cellspacing= cellpadding=>

<tr>

<td>网站首页</td>

</tr>

</table>

<!  >

<table width=% border= cellspacing= cellpadding=>

<tr>

<td>版权所有网站梦工厂</td>

</tr>

</table>

<!  >

<script src=script/Templateclassjs></script>

<script>

var tmplt=new Template();

var root=locationhref;

root=unescape(rootsubstring(rootlastIndexOf(/)+));

tmpltset_root(root);

tmpltset_file(fhtpl/);

tmpltset_file(headtpl/);

tmpltset_file(foottpl/);

tmpltset_block(fhBROWSrows);

tmpltset_block(BROWSBCOLScols);

tmpltset_var(WELCOME欢迎光临);

for(var i=;i<;i++)

{

tmpltset_var(cols);

for(var j=;j<;j++)

{

tmpltset_var(NUMBERi++j);

tmpltparse(colsBCOLStrue);

}

tmpltparse(rowsBROWStrue);

}

tmpltparse(HEADheadfalse);

tmpltparse(FOOTfootfalse);

tmpltpparse(outfhfalse);

</script>

               

上一篇:关于include标签导致js路径找不到的问题分析及解决

下一篇:javascript通过调用doPostBack回传页面