Created
July 15, 2013 07:11
-
-
Save Gaubee/5997996 to your computer and use it in GitHub Desktop.
一种模板解析的思路
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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