├── LICENSE ├── README.md ├── index.js └── package.json /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2008 John Resig 4 | Copyright (c) 2015 davidjamesstone 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # resig 2 | JavaScript Micro-Templating from John Resig 3 | [JavaScript Micro-Templating](http://ejohn.org/blog/javascript-micro-templating/) 4 | 5 | 6 | Super-simple templating function that is fast, caches quickly, and is easy to use in the browser or nodejs. 7 | 8 | ```html 9 | 19 | ``` 20 | 21 | You would use it from script like so: 22 | ```js 23 | var resig = require('resig'); 24 | var results = document.getElementById('results'); 25 | results.innerHTML = resig('tmpl', dataObject); 26 | ``` 27 | 28 | You could pre-compile the results for later use. If you call the templating function with only an ID (or a template code) then it’ll return a pre-compiled function that you can execute later: 29 | ```js 30 | var userTmpl = resig('tmpl'), html = ''; 31 | for (var i = 0; i < users.length; i++ ) { 32 | html += userTmpl(users[i]); 33 | } 34 | ``` 35 | 36 | Finally, here's the code in full: 37 | ```js 38 | // Simple JavaScript Templating 39 | // John Resig - http://ejohn.org/ - MIT Licensed 40 | (function(){ 41 | var cache = {}; 42 | 43 | this.tmpl = function tmpl(str, data){ 44 | // Figure out if we're getting a template, or if we need to 45 | // load the template - and be sure to cache the result. 46 | var fn = !/\W/.test(str) ? 47 | cache[str] = cache[str] || 48 | tmpl(document.getElementById(str).innerHTML) : 49 | 50 | // Generate a reusable function that will serve as a template 51 | // generator (and which will be cached). 52 | new Function("obj", 53 | "var p=[],print=function(){p.push.apply(p,arguments);};" + 54 | 55 | // Introduce the data as local variables using with(){} 56 | "with(obj){p.push('" + 57 | 58 | // Convert the template into pure JavaScript 59 | str 60 | .replace(/[\r\t\n]/g, " ") 61 | .split("<%").join("\t") 62 | .replace(/((^|%>)[^\t]*)'/g, "$1\r") 63 | .replace(/\t=(.*?)%>/g, "',$1,'") 64 | .split("\t").join("');") 65 | .split("%>").join("p.push('") 66 | .split("\r").join("\\'") 67 | + "');}return p.join('');"); 68 | 69 | // Provide some basic currying to the user 70 | return data ? fn( data ) : fn; 71 | }; 72 | })(); 73 | ``` 74 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = (function() { 2 | var cache = {}; 3 | 4 | function tmpl(str, data) { 5 | // Figure out if we're getting a template, or if we need to 6 | // load the template - and be sure to cache the result. 7 | var fn = !/\W/.test(str) ? 8 | cache[str] = cache[str] || 9 | tmpl(document.getElementById(str).innerHTML) : 10 | 11 | // Generate a reusable function that will serve as a template 12 | // generator (and which will be cached). 13 | new Function("obj", 14 | "var p=[],print=function(){p.push.apply(p,arguments);};" + 15 | 16 | // Introduce the data as local variables using with(){} 17 | "with(obj){p.push('" + 18 | 19 | // Convert the template into pure JavaScript 20 | str 21 | .replace(/[\r\t\n]/g, " ") 22 | .split("<%").join("\t") 23 | .replace(/((^|%>)[^\t]*)'/g, "$1\r") 24 | .replace(/\t=(.*?)%>/g, "',$1,'") 25 | .split("\t").join("');") 26 | .split("%>").join("p.push('") 27 | .split("\r").join("\\'") + "');}return p.join('');"); 28 | 29 | // Provide some basic currying to the user 30 | return data ? fn(data) : fn; 31 | }; 32 | 33 | return tmpl; 34 | })(); 35 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "resig", 3 | "version": "1.0.1", 4 | "description": "JavaScript Micro-Templating from John Resig http://ejohn.org/blog/javascript-micro-templating/", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/davidjamesstone/resig" 12 | }, 13 | "keywords": [ 14 | "Template", 15 | "Engine", 16 | "ERB-style javascript templates", 17 | "EJS-style javascript templates", 18 | "ASP-style javascript templates", 19 | "Microtemplate" 20 | ], 21 | "author": "davidjamesstone / johnresig", 22 | "license": "MIT", 23 | "bugs": { 24 | "url": "https://github.com/davidjamesstone/resig/issues" 25 | }, 26 | "homepage": "https://github.com/davidjamesstone/resig" 27 | } 28 | --------------------------------------------------------------------------------