[self-made template engine] add directive

Source: Internet
Author: User

One of the most important features of the template engine is its instruction system, which allows the template engine to have computational functions by executing some simple expressions, but with instructions that allow the template engine to have more powerful functions, try to implement some simple instructions, such as for,if, etc.

Extending the previous section of the code that iterates over the nodes, I need to do special processing of these built-in instructions as I traverse the nodes, assuming that the instructions are to the presence of the <Div cc-for= "Arr" ></div> :

    functionEachnode (ele,obj) {//See if a property needs to be converted to its own properties        varAttrs =ele.attributes; varChildNodes =Ele.childnodes; ForEach (Attrs,function(attr,index) {if(Perfix.exec (attr.nodename)) {//there is a property for the transformation, whether it needs to be evaluatedSetTimeout (function(){                    //Put the property handler in the execution queue                    //avoid contaminating the original DOM structure during executionAttrhandle (ATTR.NODENAME.SUBSTR (3), ele,attr.value); },0)            }        }); if(Ele.getattribute ("Cc-each")) {            return; }        returnEle; }
View Code

Regardless of the form, it is important to find them and respond to them, so create a function that handles them.

    functionAttrhandle (type,node,value) {varNEWOBJ =buildexpression (value,originobj); Switch(type) { Case"Each" :                //Create a document fragment                varFragment =document.createdocumentfragment (); vari = 0; varl; varNewNode; Node.removeattribute ("Cc-each"); //determine whether an array or an object                if(Object.prototype.toString.call (NEWOBJ) = = = "[Object Array]"){                     for(L = newobj.length;i<l;i++) {NewNode= Node.clonenode (true);                    Fragment.appendchild (Eachnode (newnode,newobj[i));                } node.parentNode.replaceChild (Fragment,node); }Else{                     for(varKinchNEWOBJ) {NewNode= Node.clonenode (true);                    Fragment.appendchild (Eachnode (newnode,newobj[k));                } node.parentNode.replaceChild (Fragment,node); }                 Break;  Case"Click": Node.addeventlistener ("Click", NEWOBJ,false);  Break;  Case"If" :                if(NEWOBJ) {Node.style.display= "Block"; }Else{Node.style.display= "None"; }                 Break; }    }    

Last to GitHub address:[email protected]: Taixw2/temptale.git

This series ended hastily, and I found it more tiring to manage a blog than to write code.

[self-made template engine] add directive

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.