用javascript做模版的话,说来也算是比较方便的,特别是对于PHP开发来说,只要扔一个json数组过来。然后剩下的就可以让javascript来完成了。
搜索一下jQuery的plugin,可以找到大约5~6个模版程序。好象用的比较多的还是jTemplate,上一次司徒正美用javascript写了一个简单的例子,这次又写了一个比较详细的,说是v2,有兴趣的朋友可以尝试一下。。
--start--司徒正美认为模版要处理复杂的玩意,所以写的功能就强大了。
本版本主要是对原模板系统进行提速,去掉消耗巨大的辅助函数。本来想用它与John Resig的 Micro-Templating比较一下速度,发现对方无法处理复杂的模板,残念。
JavaScript代码
-
- (function () {
- if(!String.prototype.trim){
- String.prototype.trim = function(str) {
- return this.replace(/^[\s\xa0]+|[\s\xa0]+$/g, '');
- }
- }
- var dom = {
- quote: function (str) {
- str = str.replace(/[\x00-\x1f\\]/g, function (chr) {
- var special = metaObject[chr];
- return special ? special : '\\u' + ('0000' + chr.charCodeAt(0).toString(16)).slice(-4)
- });
- return '"' + str.replace(/"/g, '\\"') + '"';
- }
- },
- metaObject = {
- '\b': '\\b',
- '\t': '\\t',
- '\n': '\\n',
- '\f': '\\f',
- '\r': '\\r',
- '\\': '\\\\'
- },
- parser = document.createElement("div"),
- startOfHTML = "\t__views.push(",
- endOfHTML = ");\n";
-
- //onsite,可选,Boolean,是否就地替换掉模板容器,默认true,如果为false,则返回一个文档碎片,交由用户自己插入到需要的地方
- dom.ejs = function (obj) {
- var onsite = obj.onsite === void 0 ,
- left = obj.left || "<%",
- right =obj.right || "%>",
- selector = obj.selector,
- isLeft = true,
- buff = ["var __views = [];\n"],
- fragment = document.createDocumentFragment(),
- el = document.getElementById(selector),
- ejs = dom.ejs;
- if (!el) throw "找不到目标元素";
- var str = el.text.trim();
- if(!ejs[selector]){
- while(str.length){
- var condition = isLeft ? left :right;
- var index = str.indexOf(condition);
- if(index !== -1){//取左边
- var text = str.slice(0,index);
- if(isLeft){
- buff.push(startOfHTML, dom.quote(text.trim()), endOfHTML);
- }else{
- switch (text.charAt(0)) {
- case "#"://处理注释
- break;
- case "="://处理后台返回的变量(输出到页面的);
- buff.push(startOfHTML, text.slice(1), endOfHTML)
- break;
- default:
- buff.push(text, "\n")
- };
- }
- }else{
- if(isLeft){
- buff.push(startOfHTML, dom.quote(str), endOfHTML);
- break;
- }else{
- throw "在字符串{{ "+dom.quote(str)+" }}中找不到右界定符"+right
- }
- }
- str = str.slice(index+2).trim();
- isLeft = !isLeft;
- }
- ejs[selector] = new Function("json", "with(json){"+buff.join("") + '};return __views.join("");')
- }
- parser.innerHTML = ejs[selector](obj.json || {});
- while (parser.firstChild) {
- fragment.appendChild(parser.firstChild)
- }
- return onsite ? el.parentNode.replaceChild(fragment, el) : fragment;
- };
- window.dom = dom;
-
- })();
这种使用原生代码写的例子,可以被任何代码所使用,如果你有兴趣也可以看看司徒正美的例子的。原文网址在javascript 模板系统 ejs v2,可以移步一观。
纯记录: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
Flash and web site templates. Great collection with some attractive and modern designs to choose from.
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
Just 9 templates here but if your looking for free fast loading quality XHTML & CSS templates, this site is a must!
Solucija
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
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
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
A large collection of attractive designs released under a Creative Commons Attribution 2.5 license by the metamorphosis designers.
Iron Myers
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
A list of 11 extremely fast loading quality templates fully xhtml 1.1 and css compliant.
Nice and Free CSS Templates
Just copy and paste any of the 12 layouts, all templates are XHTML 1.0 and CSS2/ tableless.
CSSTemps
About 60 free valid XHTML/CSS templates. All templates are protected by the license inside the template.
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
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
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
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
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
Download and upload free web designs, over 3000 demo’s to browse through!
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
A collection of 2000+ web designs submitted by the community that anyone can download free of charge!
CSS 4 Free
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
Free Open Source XHTML/CSS templates designed with true web 2.0 standard and 100% W3C validated.
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
Free templates catalogue that links only to high quality and free templates that can be 100% downloaded.
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
Studio 7 designs all free CSS/XHTML website templates are free to use under the Creative Commons Attribution 2.5 license
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
Premier source with 527 free website templates and layouts created specifically for CMSimple.
Themebot
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
Dozens of website templates, website layouts, newsletter templates and logo templates, that you can download free of charge.
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
Nice collection of free web and flash templates for personal and business use. Some templates include the .PSD files.
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
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
About 100 cool web, flash and CSS templates 100% free. Link back required.
Mashable
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
Gallery of 150 professional looking websites free of charge! Link back required.
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
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
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
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.