├── .gitignore ├── .npmignore ├── .travis.yml ├── lib └── tmpl.js ├── license ├── package.json ├── readme.md └── test └── tmpl-test.js /.gitignore: -------------------------------------------------------------------------------- 1 | /.tags 2 | /lib-cov/ 3 | /node_modules/ 4 | /npm-debug.log 5 | /package-lock.json 6 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .gitignore 2 | .npmignore 3 | .tags 4 | .travis.yml 5 | lib-cov/ 6 | node_modules/ 7 | npm-debug.log 8 | test/ 9 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 0.6 4 | -------------------------------------------------------------------------------- /lib/tmpl.js: -------------------------------------------------------------------------------- 1 | var INTERPOLATE = /{([^{]+?)}/g 2 | 3 | module.exports = function(str, data) { 4 | var tmpl = 'var __p=[],print=function(){__p.push.apply(__p,arguments);};' + 5 | 'with(obj||{}){__p.push(\'' + 6 | str.replace(/\\/g, '\\\\') 7 | .replace(/'/g, "\\'") 8 | .replace(INTERPOLATE, function(match, code) { 9 | return "'," + code.replace(/\\'/g, "'") + ",'" 10 | }) 11 | .replace(/\r/g, '\\r') 12 | .replace(/\n/g, '\\n') 13 | .replace(/\t/g, '\\t') 14 | + "');}return __p.join('');" 15 | var func = new Function('obj', tmpl) 16 | return data ? func(data) : func 17 | } 18 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | BSD License 2 | 3 | Copyright (c) 2014, Naitik Shah. All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without modification, 6 | are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright notice, this 9 | list of conditions and the following disclaimer. 10 | 11 | * Redistributions in binary form must reproduce the above copyright notice, 12 | this list of conditions and the following disclaimer in the documentation 13 | and/or other materials provided with the distribution. 14 | 15 | * Neither the name Naitik Shah nor the names of its contributors may be used to 16 | endorse or promote products derived from this software without specific 17 | prior written permission. 18 | 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 20 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 21 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 22 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 23 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 26 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 28 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "tmpl", 3 | "description": "JavaScript micro templates.", 4 | "version": "1.0.5", 5 | "license": "BSD-3-Clause", 6 | "homepage": "https://github.com/daaku/nodejs-tmpl", 7 | "author": "Naitik Shah ", 8 | "main": "lib/tmpl", 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/daaku/nodejs-tmpl" 12 | }, 13 | "scripts": { 14 | "test": "NODE_PATH=./lib mocha --ui exports" 15 | }, 16 | "devDependencies": { 17 | "mocha": "^9.1.1" 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | tmpl [![Build Status](https://secure.travis-ci.org/nshah/nodejs-tmpl.png)](http://travis-ci.org/nshah/nodejs-tmpl) 2 | ==== 3 | 4 | Simple string formatting using `{}`. 5 | 6 | ```javascript 7 | assert.equal( 8 | tmpl('the answer is {answer}', { answer: 42 }), 9 | 'the answer is 42') 10 | ``` 11 | -------------------------------------------------------------------------------- /test/tmpl-test.js: -------------------------------------------------------------------------------- 1 | var tmpl = require('../lib/tmpl') 2 | , assert = require('assert') 3 | 4 | exports['basic name substitution'] = function() { 5 | assert.equal( 6 | tmpl('the answer is {answer}', { answer: 42 }), 7 | 'the answer is 42') 8 | } 9 | --------------------------------------------------------------------------------