手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表Tag:模版

javascript模版系统

用javascript做模版的话,说来也算是比较方便的,特别是对于PHP开发来说,只要扔一个json数组过来。然后剩下的就可以让javascript来完成了。
搜索一下jQuery的plugin,可以找到大约5~6个模版程序。好象用的比较多的还是jTemplate,上一次司徒正美用javascript写了一个简单的例子,这次又写了一个比较详细的,说是v2,有兴趣的朋友可以尝试一下。。
--start--司徒正美认为模版要处理复杂的玩意,所以写的功能就强大了。
本版本主要是对原模板系统进行提速,去掉消耗巨大的辅助函数。本来想用它与John Resig的 Micro-Templating比较一下速度,发现对方无法处理复杂的模板,残念。

JavaScript代码
  1. //司徒正美 javascript template - http://www.cnblogs.com/rubylouvre/ - MIT Licensed  
  2.      (function () {  
  3.                if(!String.prototype.trim){  
  4.                    String.prototype.trim = function(str) {  
  5.                        return this.replace(/^[\s\xa0]+|[\s\xa0]+$/g, '');  
  6.                    }  
  7.                }  
  8.                var dom = {  
  9.                    quote: function (str) {  
  10.                        str = str.replace(/[\x00-\x1f\\]/g, function (chr) {  
  11.                            var special = metaObject[chr];  
  12.                            return special ? special : '\\u' + ('0000' + chr.charCodeAt(0).toString(16)).slice(-4)  
  13.                        });  
  14.                        return '"' + str.replace(/"/g, '\\"') + '"';  
  15.                    }  
  16.                },  
  17.                metaObject = {  
  18.                    '\b''\\b',  
  19.                    '\t''\\t',  
  20.                    '\n''\\n',  
  21.                    '\f''\\f',  
  22.                    '\r''\\r',  
  23.                    '\\': '\\\\' 
  24.                }, 
  25.                parser = document.createElement("div"), 
  26.                startOfHTML = "\t__views.push(", 
  27.                endOfHTML = ");\n"; 
  28.            
  29.                //onsite,可选,Boolean,是否就地替换掉模板容器,默认true,如果为false,则返回一个文档碎片,交由用户自己插入到需要的地方 
  30.                dom.ejs = function (obj) { 
  31.                    var onsite = obj.onsite === void 0 , 
  32.                    left = obj.left || "<%", 
  33.                    right =obj.right || "%>", 
  34.                    selector = obj.selector, 
  35.                    isLeft = true, 
  36.                    buff = ["var __views = [];\n"], 
  37.                    fragment = document.createDocumentFragment(), 
  38.                    el = document.getElementById(selector), 
  39.                    ejs = dom.ejs; 
  40.                    if (!el) throw "找不到目标元素"; 
  41.                    var str = el.text.trim(); 
  42.                    if(!ejs[selector]){ 
  43.                        while(str.length){ 
  44.                            var condition = isLeft ? left :right; 
  45.                            var index = str.indexOf(condition); 
  46.                            if(index !== -1){//取左边 
  47.                                var text = str.slice(0,index); 
  48.                                if(isLeft){ 
  49.                                    buff.push(startOfHTML, dom.quote(text.trim()), endOfHTML); 
  50.                                }else{ 
  51.                                    switch (text.charAt(0)) { 
  52.                                        case "#"://处理注释 
  53.                                            break; 
  54.                                        case "="://处理后台返回的变量(输出到页面的); 
  55.                                            buff.push(startOfHTML, text.slice(1), endOfHTML) 
  56.                                            break; 
  57.                                        default: 
  58.                                            buff.push(text, "\n") 
  59.                                    }; 
  60.                                } 
  61.                            }else{ 
  62.                                if(isLeft){ 
  63.                                    buff.push(startOfHTML, dom.quote(str), endOfHTML); 
  64.                                    break; 
  65.                                }else{ 
  66.                                    throw "在字符串{{ "+dom.quote(str)+" }}中找不到右界定符"+right 
  67.                                } 
  68.                            } 
  69.                            str = str.slice(index+2).trim(); 
  70.                            isLeft = !isLeft; 
  71.                        } 
  72.                        ejs[selector] = new Function("json", "with(json){"+buff.join("") + '};return __views.join("");')  
  73.                    }  
  74.                    parser.innerHTML = ejs[selector](obj.json || {});  
  75.                    while (parser.firstChild) {  
  76.                        fragment.appendChild(parser.firstChild)  
  77.                    }  
  78.                    return onsite ? el.parentNode.replaceChild(fragment, el) : fragment;  
  79.                };  
  80.                window.dom = dom;  
  81.   
  82.            })();  

这种使用原生代码写的例子,可以被任何代码所使用,如果你有兴趣也可以看看司徒正美的例子的。原文网址在javascript 模板系统 ejs v2,可以移步一观。

Tags: jquery, template, 模版, 模板

免费模版网站列表

纯记录:http://gofreeserve.info/archives/free-web-template-resources

Check out these 40 excellent websites that offer W3 standard-compliant website templates and flash template pages for free download without any registration or sign-up!

Simavera Website Templates
Simavera Website Templates
Flash and web site templates. Great collection with some attractive and modern designs to choose from.

Open Designs
Open Designs
XHTML and CSS based free web design templates. Over 1100 open source designs available for download and growing by the day!

Tristar Web Design
Tristar Web Design Templates
Just 9 templates here but if your looking for free fast loading quality XHTML & CSS templates, this site is a must!

Solucija
Solucija Templates
Free XHTML/CSS website templates available to use without any limitations. They can be used on a non commercial site as well as on a commercial one. Keeping a small credit link in the footer is not obligatory. All templates are hand coded and lightweight (under 60KB) which ensures fast loading.

Styleshout
Styleshout Templates
Large collection of quality free website templates for download. All templates are valid xthml(strict) and valid CSS, and distributed under a Creative Commons Attribution 2.5 license

Dream Template
Dream Template
334 free templates but users must at all times have the pre-installed DreamTemplate linking button/text on the front page of their web site. This policy does NOT apply to registered/paid members.

Metamorphozis Design
Metamorphosis Design Templates
A large collection of attractive designs released under a Creative Commons Attribution 2.5 license by the metamorphosis designers.

Iron Myers
Iron Myers Templates
Simplify your site designs with 168 of the most useful Grid and CSS Layouts. They are perfect for website templates, Blog themes, CMS or as a starting point for your web projects. Layouts offer full Grade-A browser support, That is, the layouts will look and behave the same in all internet browsers.

Six Shooter Media
Six Shooter Media Templates
A list of 11 extremely fast loading quality templates fully xhtml 1.1 and css compliant.

Nice and Free CSS Templates
Nice and Free CSS Templates
Just copy and paste any of the 12 layouts, all templates are XHTML 1.0 and CSS2/ tableless.

CSSTemps
CSS Temps
About 60 free valid XHTML/CSS templates. All templates are protected by the license inside the template.

Free CSS Templates
Free CSS Templates
All of these templates are released for FREE under the Creative Commons Attribution 2.5 license for any type of personal or commercial use provided you follow the license. Link back required.

Clan Templates
Clan Templates
Free templates for on-line gaming clans for games with multiplayer options. Clan Templates provides free gaming layouts and graphics to gamers and designers alike.

Free Website Templates
Free Website Templates
Claims to be the best free website templates on the internet and you don’t need to link back, but you should state that you used a template originally made by Free Website Templates.

Template World
Template World
Absolutely top quality, unique and creative web site XHTML/CSS templates designed by Template World. Templates have been tested and proven compatible with all major browser environments and operating systems. Released under Creative Commons Attribution 2.5 license you are free to modify but you must link back to Template World.

Layout Gala
Layout Gala
A collection of 40 CSS layouts based on the same markup and ready for download! If you want, you can download the entire collection, 40 HTML pages in a single zip file.

Open Web Design
Open Web Design
Download and upload free web designs, over 3000 demo’s to browse through!

Free CSS
Free CSS
All of the 1000 templates or so that are featured within Free CSS are distributed under some form of GNU/GPL License or under a Creative Commons License.

Open Source Web Design
Open Source Web Design
A collection of 2000+ web designs submitted by the community that anyone can download free of charge!

CSS 4 Free
CSS 4 Free Templates
Free CSS based website templates gallery for you to preview, rate, comment on, and download. Most of the templates are licensed under Creative Commons 2.5 or GPL.

Open Source Web Templates
Open Source Web Templates
Free Open Source XHTML/CSS templates designed with true web 2.0 standard and 100% W3C validated.

Premium Website Templates
Premium Website Templates
Tableless web templates built using XHTML and CSS with some also available as Blogger template or Wordpress theme. All released under a Creative Commons Attribution 2.5 license

Template Navigator
Template Navigator
Free templates catalogue that links only to high quality and free templates that can be 100% downloaded.

Templates Box
Templates Box
A large inventory of free website and flash templates available. Link back is required, downloads include the .PSD files and fonts.

Open Source Templates
Open Source Templates
Studio 7 designs all free CSS/XHTML website templates are free to use under the Creative Commons Attribution 2.5 license

Free Layouts
Free Layouts
Offers several hundred free W3 standard-compliant website templates and flash template pages for download. The site has some older templates in the collection that may not validate, and as such, you should make sure your code validates before using the template.

Dotcom Webdesign
Dotcom Design Templates
Premier source with 527 free website templates and layouts created specifically for CMSimple.

Themebot
Themebot Templates
Portal helping users to find themes/templates/styles/skins for various content management systems, blog and forum software. Free high quality open source website templates using XHTML and CSS.

Interspire
Interspire Templates
Dozens of website templates, website layouts, newsletter templates and logo templates, that you can download free of charge.

Free CSS Templates
Free CSS Templates
One of the biggest, never before seen collection of free CSS Templates on the web! All these CSS templates are released under the Creative Commons Attribution 2.5 license. At present, there are 219 free CSS templates downloadable in .Zip format.

All Free Templates
All Free Templates
Nice collection of free web and flash templates for personal and business use. Some templates include the .PSD files.

Template Workz
Template Workz
Templates are free to download and use for your personal and business projects. Website templates are available in Photoshop (PSD) and HTML formats, all the fonts are also included with the PSD file. Link back only required if you use the downloaded graphics.

Snakeye Dreamweaver Web Templates
Snakeye Dreamweaver Web Templates
20 free Dreamweaver web site templates and web page templates for download. May be used for any web site, provided the tagline at the base of the template remains within the template design.

TemplateYes
TemplateYes
About 100 cool web, flash and CSS templates 100% free. Link back required.

Mashable
Mashable Templates
A collection of 150+ great free templates from around the web including 40+ one column layouts, 80+ two column layouts and 30+ three column layouts.

Free Templates Online
Free Templates Online
Gallery of 150 professional looking websites free of charge! Link back required.

Web Design Company
Web Design Company
Unique and professional Web site templates, Flash templates, HTML templates including the full set of sub-pages free of cost. Submit name and email prior to download and link back required.

Andreas Viklund
Andreas Viklund Templates
All templates are made with valid XHTML (strict) and CSS, and they are supposed to be small, easy to work with and of high code quality. Templates are free to use for any purpose without any limitations, obligations or conditions.

Zymic
Zymic Templates
Preview live and download any one of the 90+ top quality free web designs and layouts. Link back is required!

Free online web template generator
doTemplate Generator
Easily create unique professional web templates: Pick Template > Customise > Download!
doTemplate offers a catalogue of free web templates. All templates are fully customizable to your taste and needs. You can easily tweak the colors, the fonts, apply a bunch of graphical effects to create a unique professional template in no time.

Tags: 模版, 免费