Javascript page templatification many people have never used _ javascript skills

Source: Internet
Author: User
I encountered a problem today. This problem is also a problem I encountered before. The previous method is also used by most people. You can refer to my previous article-I guess most people use it.

For example, the following code is taken from a piece of js Code on the Sina homepage. This method is also used by most people.



What did you do after reading the above Code? You may change the form of + = to the push form of the array. Some people say that push is faster than this character link, but it is not much faster.

If there is a lot of embedded html code, do you want to handle it like this? If you modify (add or delete) an html, You need to constantly flip the code, find the corresponding tag, and then find the end tag. Are you not in trouble? Some people may write like this (this is what I wrote earlier, and it is also my most used method ).

The above code is also very difficult to maintain. Many times, if a tag is added to the ui or the html structure is modified, it is a headache for me.

Page templatification-A New Method

I recently read facebook's js Code. By chance, I found that the following method looks good and I hope to share it with you.

View my index.html code

View my js Code

It is easy to maintain the preceding html code. Next time, no matter what changes are made to the html code structure, it will be easy to change.

Summary

The above code has many problems. Don't be surprised. This is just my simple version.

The current template only supports simple variable replacement, and many functions are not developed, such as the if tag and for tag. if you are interested, try it. if it is successful, let me know. If we can build an open-source framework similar to smarty, we can also make some contributions to the open-source business.

Author: Pangolin

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.