JavaScript template engine usage examples _javascript tips

Source: Internet
Author: User

The examples in this article describe the JavaScript template engine usage. Share to everyone for your reference. Specifically as follows:

The template engine described here is very short and very well worth a visit.

Tmpl.js files are as follows:

Simple JavaScript Templating//John resig-http://ejohn.org/-MIT licensed (function () {var cache = {}; This.tmpl = function Tmpl (str, data) {//Figure out if we ' re getting a template, or if we need to//load the Templat
  E-and Be sure to cache the result. 
   var fn =!/\w/.test (str)? CACHE[STR] = Cache[str] | | Tmpl (document.getElementById (str). InnerHTML)://Generate A reusable function that would serve as a template//
   Generator (and which would be cached). New Function ("obj", "var p=[],print=function () {p.push.apply (p,arguments);};" +//introduce the data as local Var Iables using with () {} ' with (obj) {p.push (' +//Convert the template into pure JavaScript str. replace (/[ \r\t\n]/g, ""). Split ("<%"). Join ("\ T"). Replace ((^|%>) [^\t]*) '/g, ' $1\r '). Replace (/\t= (. *?)
     %>/g, "', $, '"). Split ("T"). Join ("');") . Split ("%>"). Join ("P.push"). Split ("\ r"). Join ("\ \") + 
    "');} return P.join ('); "); Function ends//provide some basic currying to the user return data?
 fn (data): FN;
};

 })();

The

index.html file is as follows:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >  

The

wants this article to help you with your JavaScript programming.

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.