├── .travis.yml ├── .gitignore ├── package.json ├── nuts.js ├── tests └── nuts.spec.js ├── LICENSE └── README.md /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.12" 4 | - "0.11" 5 | - "0.10" 6 | 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # Compiled binary addons (http://nodejs.org/api/addons.html) 20 | build/Release 21 | 22 | # Dependency directory 23 | # Commenting this out is preferred by some people, see 24 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- 25 | node_modules 26 | 27 | # Users Environment Variables 28 | .lock-wscript 29 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "NUTS", 3 | "version": "1.0.0", 4 | "description": "Nearly Useless Template System", 5 | "main": "nuts.js", 6 | "dependencies": {}, 7 | "devDependencies": { 8 | "mocha": "^2.2.1" 9 | }, 10 | "scripts": { 11 | "test": "mocha tests" 12 | }, 13 | "repository": { 14 | "type": "git", 15 | "url": "https://github.com/letsgetrandy/NUTS.git" 16 | }, 17 | "keywords": [ 18 | "javascript", 19 | "template", 20 | "templating", 21 | "nuts" 22 | ], 23 | "author": "Randy Hunt", 24 | "license": "MIT", 25 | "bugs": { 26 | "url": "https://github.com/letsgetrandy/NUTS/issues" 27 | }, 28 | "homepage": "https://github.com/letsgetrandy/NUTS" 29 | } 30 | -------------------------------------------------------------------------------- /nuts.js: -------------------------------------------------------------------------------- 1 | (function() { 2 | 'use strict'; 3 | 4 | /** 5 | * super-simple templating 6 | */ 7 | 8 | function NUTS(template, context) { 9 | return template.replace(/\[:(\w+)\]/g, function(match, key) { 10 | if (context[key] instanceof Function) { 11 | return context[key](); 12 | } else { 13 | return context[key]; 14 | } 15 | }); 16 | } 17 | 18 | (function() { 19 | if (typeof define === 'function' && typeof define.amd === 'object') { 20 | define(function() { 21 | return NUTS; 22 | }); 23 | } else if (typeof module !== 'undefined' && module.exports) { 24 | module.exports = NUTS; 25 | } else if (typeof window !== 'undefined') { 26 | window.NUTS = NUTS; 27 | } 28 | })(); 29 | })(); 30 | 31 | -------------------------------------------------------------------------------- /tests/nuts.spec.js: -------------------------------------------------------------------------------- 1 | var NUTS = require('../nuts'), 2 | assert = require('assert'); 3 | 4 | describe('templatize()', function() { 5 | 'use strict'; 6 | 7 | it('should work with properties', function() { 8 | 9 | // example usage 10 | var object = { 11 | "foo": "hello", 12 | "bar": "world", 13 | "punctuation": function() { 14 | return "!"; 15 | } 16 | }, 17 | template = '[:foo] [:bar][:punctuation]', 18 | s = NUTS(template, object); 19 | 20 | assert.equal(s, 'hello world!'); 21 | 22 | }); 23 | 24 | it('should work with functions', function() { 25 | 26 | var model = { 27 | "firstName": "Fred", 28 | "lastName": "Flintstone", 29 | "name": function() { 30 | return this.firstName + ' ' + this.lastName; 31 | } 32 | }, 33 | template = '[:name]', 34 | s = NUTS(template, model);; 35 | 36 | assert.equal(s, 'Fred Flintstone'); 37 | }); 38 | }); 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Randy Hunt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [](https://travis-ci.org/letsgetrandy/NUTS) 2 | [](https://www.codacy.com/app/letsgetrandy/NUTS) 3 | [](https://codeclimate.com/repos/555a0d6ce30ba01b36001438/feed) 4 | [](https://www.bithound.io/github/letsgetrandy/NUTS) 5 | 6 | # NUTS 7 | Nearly Useless Template System 8 | 9 | NUTS is a _nearly useless_ templating system for Javascript. 10 | 11 | ## Why use it? 12 | Having functionality in your template system creates more possible points of failure, making it harder to test and harder to find bugs when they exist. It's better if your functionality is inserted and removed from some other, testable member, while your templates hang around behind the scene just spitting out the end result. 13 | 14 | ## How to use it 15 | NUTS is simple to use. 16 | 17 | First, create a template: 18 | 19 | ```js 20 | var template = '