diff --git a/README.md b/README.md index 324b943af..10ed9ce4d 100644 --- a/README.md +++ b/README.md @@ -99,13 +99,13 @@ Hello ${data.name}! The template can then be rendered as shown in the following sample code: ```javascript -var raptorTemplates = require('raptor-templates'); var templatePath = require.resolve('./hello.rhtml'); - -raptorTemplates.render(templatePath, { +var template = require('raptor-templates').load(templatePath); +template.render({ name: 'World', colors: ["red", "green", "blue"] - }, function(err, output) { + }, + function(err, output) { console.log(output); }); ``` @@ -205,8 +205,8 @@ npm install raptor-templates --global ### Callback API ```javascript -var raptorTemplates = require('raptor-templates'); -raptorTemplates.render('template.rhtml', { +var template = require('raptor-templates').load('template.rhtml'); +template.render({ name: 'Frank', count: 30 }, @@ -222,12 +222,11 @@ raptorTemplates.render('template.rhtml', { ### Streaming API ```javascript -var raptorTemplates = require('raptor-templates'); +var template = require('raptor-templates').load('template.rhtml'); var out = require('fs').createWriteStream('index.html', 'utf8'); // Render the template to 'index.html' -raptorTemplates - .stream('template.rhtml', { +template.stream({ name: 'Frank', count: 30 }) @@ -239,6 +238,7 @@ raptorTemplates ```javascript var raptorTemplates = require('raptor-templates'); +var template = raptorTemplates.load('template.rhtml'); var out = require('fs').createWriteStream('index.html', 'utf8'); var context = raptorTemplates.createContext(out); @@ -251,10 +251,8 @@ setTimeout(function() { }, 1000); // Render the template to the existing render context: -raptorTemplates - .render( - 'template.rhtml', - { +template + .render({ name: 'World' }, context); @@ -280,11 +278,16 @@ Given the following module code that will be used to render a template on the cl _run.js_: ```javascript -var raptorTemplates = require('raptor-templates'); + var templatePath = require.resolve('./hello.rhtml'); -raptorTemplates.render(templatePath, {name: 'John'}, function(err, output) { - document.body.innerHTML = output; -}); +var template = require('raptor-templates').load(templatePath); + +templatePath.render({ + name: 'John' + }, + function(err, output) { + document.body.innerHTML = output; + }); ``` You can then bundle up the above program for running in the browser using either [raptor-optimizer](https://github.com/raptorjs3/raptor-optimizer) (recommended) or [browserify](https://github.com/substack/node-browserify). @@ -1166,7 +1169,8 @@ The complete code for this example is shown below: _components/tabs/renderer.js:_ ```javascript -var raptorTemplates = require('raptor-templates'); +var templatePath = require.resolve('./template.rhtml'); +var template = require('raptor-templates').load(templatePath); module.exports = function render(input, context) { var nestedTabs = []; @@ -1180,10 +1184,9 @@ module.exports = function render(input, context) { }); // Now render the markup for the tabs: - raptorTemplates.render(require.resolve('./template.rhtml'), { + template.render({ tabs: nestedTabs }, context); - }; ``` diff --git a/package.json b/package.json index a871acaac..155a1bf29 100644 --- a/package.json +++ b/package.json @@ -1,60 +1,61 @@ { - "name": "raptor-templates", - "description": "Raptor Templates", - "keywords": [ - "templating", - "template", - "async", - "streaming" - ], - "repository": { - "type": "git", - "url": "https://github.com/raptorjs3/raptor-templates.git" - }, - "scripts": { - "test": "node_modules/.bin/mocha --ui bdd --reporter spec ./test && node_modules/.bin/jshint compiler/ runtime/ taglibs/ dust/" - }, - "author": "Patrick Steele-Idem ", - "maintainers": [ - "Patrick Steele-Idem " - ], - "dependencies": { - "raptor-detect": "^0.2.0-beta", - "raptor-logging": "^0.2.0-beta", - "raptor-strings": "^0.2.0-beta", - "raptor-regexp": "^0.2.0-beta", - "raptor-util": "^0.2.0-beta", - "raptor-arrays": "^0.2.0-beta", - "raptor-json": "^0.2.0-beta", - "raptor-modules": "^0.2.0-beta", - "raptor-render-context": "^0.2.0-beta", - "raptor-data-providers": "^0.2.0-beta", - "raptor-xml": "^0.2.0-beta", - "raptor-objects": "^0.2.0-beta", - "raptor-ecma": "^0.2.0-beta", - "htmlparser2": "~3.5.1", - "char-props": "~0.1.5", - "raptor-promises": "^0.2.0-beta", - "raptor-args": "^0.1.9-beta", - "minimatch": "^0.2.14", - "property-handlers": "^0.2.1-beta", - "raptor-dust": "^0.3.6-beta" - }, - "devDependencies": { - "mocha": "~1.15.1", - "chai": "~1.8.1", - "jshint": "^2.5.0", - "raptor-cache": "^0.2.7-beta", - "dustjs-linkedin": "^2.3.4" - }, - "license": "Apache License v2.0", - "bin": { - "rhtmlc": "bin/rhtmlc" - }, - "main": "runtime/raptor-templates-runtime.js", - "publishConfig": { - "registry": "https://registry.npmjs.org/" - }, - "ebay": {}, - "version": "0.2.36-beta" -} \ No newline at end of file + "name": "raptor-templates", + "description": "Raptor Templates", + "keywords": [ + "templating", + "template", + "async", + "streaming" + ], + "repository": { + "type": "git", + "url": "https://github.com/raptorjs3/raptor-templates.git" + }, + "scripts": { + "test": "node_modules/.bin/mocha --ui bdd --reporter spec ./test && node_modules/.bin/jshint compiler/ runtime/ taglibs/ dust/" + }, + "author": "Patrick Steele-Idem ", + "maintainers": [ + "Patrick Steele-Idem " + ], + "dependencies": { + "raptor-detect": "^0.2.0-beta", + "raptor-logging": "^0.2.0-beta", + "raptor-strings": "^0.2.0-beta", + "raptor-regexp": "^0.2.0-beta", + "raptor-util": "^0.2.0-beta", + "raptor-arrays": "^0.2.0-beta", + "raptor-json": "^0.2.0-beta", + "raptor-modules": "^0.2.0-beta", + "raptor-render-context": "^0.2.0-beta", + "raptor-data-providers": "^0.2.0-beta", + "raptor-xml": "^0.2.0-beta", + "raptor-objects": "^0.2.0-beta", + "raptor-ecma": "^0.2.0-beta", + "htmlparser2": "~3.5.1", + "char-props": "~0.1.5", + "raptor-promises": "^0.2.0-beta", + "raptor-args": "^0.1.9-beta", + "minimatch": "^0.2.14", + "property-handlers": "^0.2.1-beta", + "raptor-dust": "^0.3.6-beta" + }, + "devDependencies": { + "mocha": "~1.15.1", + "chai": "~1.8.1", + "jshint": "^2.5.0", + "raptor-cache": "^0.2.7-beta", + "dustjs-linkedin": "^2.3.4", + "through": "^2.3.4" + }, + "license": "Apache License v2.0", + "bin": { + "rhtmlc": "bin/rhtmlc" + }, + "main": "runtime/raptor-templates-runtime.js", + "publishConfig": { + "registry": "https://registry.npmjs.org/" + }, + "ebay": {}, + "version": "0.2.36-beta" +} diff --git a/runtime/raptor-templates-runtime.js b/runtime/raptor-templates-runtime.js index 1988ade83..30aa254ed 100644 --- a/runtime/raptor-templates-runtime.js +++ b/runtime/raptor-templates-runtime.js @@ -83,7 +83,7 @@ Template.prototype = { }; } - return new Readable(this._, data); + return new Readable(this, data); } }; diff --git a/test/api-tests.js b/test/api-tests.js new file mode 100644 index 000000000..6c34c9858 --- /dev/null +++ b/test/api-tests.js @@ -0,0 +1,192 @@ +'use strict'; +var chai = require('chai'); +chai.Assertion.includeStack = true; +require('chai').should(); +var expect = require('chai').expect; +var nodePath = require('path'); +var raptorTemplates = require('../'); +var through = require('through'); + +describe('raptor-templates/rhtml' , function() { + + beforeEach(function(done) { + done(); + }); + + it('should allow a template to be loaded and rendered using a callback', function(done) { + raptorTemplates.render( + nodePath.join(__dirname, 'test-project/hello.rhtml'), + { + name: 'John' + }, + function(err, output) { + if (err) { + return done(err); + } + + expect(output).to.equal('Hello John!'); + done(); + }); + }); + + it('should allow a template to be loaded and rendered to a context wrapping a string builder', function(done) { + var context = raptorTemplates.createContext(); + context + .on('end', function() { + expect(context.getOutput()).to.equal('Hello John!'); + done(); + }) + .on('error', function(e) { + done(e); + }); + + raptorTemplates.render( + nodePath.join(__dirname, 'test-project/hello.rhtml'), + { + name: 'John' + }, + context); + + context.end(); + }); + + it('should allow a template to be loaded and rendered to a context wrapping a stream', function(done) { + var output = ''; + + var stream = through(function write(data) { + output += data; + }); + + var context = raptorTemplates.createContext(stream); + context + .on('end', function() { + expect(output).to.equal('Hello John!'); + done(); + }) + .on('error', function(e) { + done(e); + }); + + raptorTemplates.render( + nodePath.join(__dirname, 'test-project/hello.rhtml'), + { + name: 'John' + }, + context); + + context.end(); + }); + + it('should allow a template to be loaded and rendered to a stream', function(done) { + + + var output = ''; + var outStream = through(function write(data) { + output += data; + }); + + + raptorTemplates.stream( + nodePath.join(__dirname, 'test-project/hello.rhtml'), + { + name: 'John' + }) + .pipe(outStream) + .on('end', function() { + expect(output).to.equal('Hello John!'); + done(); + }) + .on('error', function(e) { + done(e); + }); + }); + + + /// TEMPLATE LOADING: + + it('should allow a template to be loaded and rendered using a callback', function(done) { + var template = raptorTemplates.load(nodePath.join(__dirname, 'test-project/hello.rhtml')); + template.render({ + name: 'John' + }, + function(err, output) { + if (err) { + return done(err); + } + + expect(output).to.equal('Hello John!'); + done(); + }); + }); + + it('should allow a template to be loaded and rendered to a context wrapping a string builder', function(done) { + var context = raptorTemplates.createContext(); + context + .on('end', function() { + expect(context.getOutput()).to.equal('Hello John!'); + done(); + }) + .on('error', function(e) { + done(e); + }); + + var template = raptorTemplates.load(nodePath.join(__dirname, 'test-project/hello.rhtml')); + template.render({ + name: 'John' + }, + context); + + context.end(); + }); + + it('should allow a template to be loaded and rendered to a context wrapping a stream', function(done) { + + var output = ''; + + var stream = through(function write(data) { + output += data; + }); + + var context = raptorTemplates.createContext(stream); + context + .on('end', function() { + expect(output).to.equal('Hello John!'); + done(); + }) + .on('error', function(e) { + done(e); + }); + + var template = raptorTemplates.load(nodePath.join(__dirname, 'test-project/hello.rhtml')); + template.render({ + name: 'John' + }, + context); + + context.end(); + }); + + it('should allow a template to be loaded and rendered to a stream', function(done) { + var template = raptorTemplates.load(nodePath.join(__dirname, 'test-project/hello.rhtml')); + + var output = ''; + var outStream = through(function write(data) { + output += data; + }); + + + template.stream({ + name: 'John' + }) + .pipe(outStream) + .on('end', function() { + expect(output).to.equal('Hello John!'); + done(); + }) + .on('error', function(e) { + done(e); + }); + }); + +}); + diff --git a/test/test-project/hello.rhtml b/test/test-project/hello.rhtml new file mode 100644 index 000000000..1422c71c7 --- /dev/null +++ b/test/test-project/hello.rhtml @@ -0,0 +1 @@ +Hello $data.name! \ No newline at end of file