Skip to content

Instantly share code, notes, and snippets.

@Gaubee
Created July 15, 2013 07:11
Show Gist options
  • Select an option

  • Save Gaubee/5997996 to your computer and use it in GitHub Desktop.

Select an option

Save Gaubee/5997996 to your computer and use it in GitHub Desktop.
一种模板解析的思路
<html>
<head>
<title>parse View</title>
</head>
<body>
<script type="template">
Hello,{{fullName}}.
<!--{{fullName}} = {{firstName}}+" "+{{lastName}}-->
<hr />
<input ms-value={{firstName}} ms-title = "姓:{{firstName}}" ></input>
<input ms-value={{lastName}} ms-title = "名字:{{lastName}}"/>
</script>
</body>
</html>
<script type="text/javascript">
var D = document;
var perStr = "ms-";
var keyMatch = new RegExp("("+perStr+"[\\w]+)[\\s]*=","g");
var viewFun = [];
function parseNode(el){
console.log(this.el = el);
var B = D.createElement("body");
B.innerHTML = el.innerHTML;
var cs = B.childNodes;
for(var i = 0,item;item=cs[i];i+=1){
if (item.nodetype===1) {
parseNode(item);
}else{
parseText(item);
}
}
}
function parseText(el){
var text = el.textContent.trim();
if (text) {
var funID = viewFun.length;
new Function("")
}
return funID
}
(function (scripts) {
for(var i = 0,item;item = scripts[i];i+=1){
if (item.type === "template") {
parseNode(item);
}
}
}(D.getElementsByTagName('script')));
</script>
Hello,{{fullName}}.
<!--{{fullName}} = {{firstName}}+" "+{{lastName}}-->
<hr />
<input ms-value={{firstName}} ms-title = "姓:{{firstName}}" ></input>
<input ms-value={{lastName}} ms-title = "名字:{{lastName}}"/>
{if {firstName}&&{lastName} }
<p>我的名字叫{{fullName}}</p>
{ {/if} }
\{([\w\s]*)(\{[\w\W]*?\})*[\s]*\}
new RegExp("\\{([\\w\\s]*)(\\{[\\w\\W]*?\\})*[\\s]*\\}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment