From 8b139199b2c78966e5f28a5799cb1cfe491f7021 Mon Sep 17 00:00:00 2001 From: Patrick Steele-Idem Date: Sat, 19 Jul 2014 13:24:13 -0600 Subject: [PATCH] Added support for template.renderSync() --- README.md | 15 +++++++++++ runtime/raptor-templates-runtime.js | 7 +++++ test/api-tests.js | 42 +++++++++++++++++++++++++++++ test/render-rhtml-tests.js | 1 - test/test-project/hello-async.rhtml | 3 +++ test/test-project/hello-error.rhtml | 1 + 6 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 test/test-project/hello-async.rhtml create mode 100644 test/test-project/hello-error.rhtml diff --git a/README.md b/README.md index 3b1e9e5ce..7bbdad95c 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ Raptor Templates is an extensible, streaming, asynchronous, [high performance](h - [Template Rendering](#template-rendering) - [Callback API](#callback-api) - [Streaming API](#streaming-api) + - [Synchronous API](#synchronous-api) - [Asynchronous Render Context API](#asynchronous-render-context-api) - [Browser-side Rendering](#browser-side-rendering) - [Using the RaptorJS Optimizer](#using-the-raptorjs-optimizer) @@ -265,6 +266,20 @@ template.render({ ``` _NOTE:_ This will end the target output stream. +### Synchronous API + +If you know that your template rendering requires no asynchronous rendering then you can use the synchronous API to render a template to a String: + +```javascript +var template = require('raptor-templates').load('template.rhtml'); + +var output = template.renderSync({ + name: 'Frank', + count: 30 + }); +console.log('Output HTML: ' + output); +``` + ### Asynchronous Render Context API ```javascript diff --git a/runtime/raptor-templates-runtime.js b/runtime/raptor-templates-runtime.js index 821d2f56f..9190db2ec 100644 --- a/runtime/raptor-templates-runtime.js +++ b/runtime/raptor-templates-runtime.js @@ -60,6 +60,13 @@ function Template(renderFunc) { } Template.prototype = { + renderSync: function(data) { + var context = new Context(); + context.sync(); + this._(data, context); + context.end(); + return context.getOutput(); + }, render: function(data, context, callback) { if (data == null) { data = {}; diff --git a/test/api-tests.js b/test/api-tests.js index 326f35351..de1029d1f 100644 --- a/test/api-tests.js +++ b/test/api-tests.js @@ -193,5 +193,47 @@ describe('raptor-templates/api' , function() { }); }); + it('should allow a template to be rendered to a string synchronously using renderSync', function() { + var template = raptorTemplates.load(nodePath.join(__dirname, 'test-project/hello.rhtml')); + var output = template.renderSync({ name: 'John' }); + expect(output).to.equal('Hello John!'); + }); + + it('should throw an error if beginAsync is used with renderSync', function() { + var template = raptorTemplates.load(nodePath.join(__dirname, 'test-project/hello-async.rhtml')); + var output; + var e; + + try { + output = template.renderSync({ + nameDataProvider: function(arg, callback) { + setTimeout(function() { + callback(null, 'John'); + }, 100); + } + }); + } catch(_e) { + e = _e; + } + + expect(output).to.equal(undefined); + expect(e).to.not.equal(undefined); + }); + + it('should throw errors correctly with renderSync', function() { + var template = raptorTemplates.load(nodePath.join(__dirname, 'test-project/hello-error.rhtml')); + var output; + var e; + + try { + output = template.renderSync(); + } catch(_e) { + e = _e; + } + + expect(output).to.equal(undefined); + expect(e).to.not.equal(undefined); + }); + }); diff --git a/test/render-rhtml-tests.js b/test/render-rhtml-tests.js index edd2f9328..ef8060709 100644 --- a/test/render-rhtml-tests.js +++ b/test/render-rhtml-tests.js @@ -392,5 +392,4 @@ describe('raptor-templates/rhtml' , function() { it("should support scanning a directory for tags", function(done) { testRender("test-project/rhtml-templates/scanned-tags.rhtml", {}, done); }); - }); diff --git a/test/test-project/hello-async.rhtml b/test/test-project/hello-async.rhtml new file mode 100644 index 000000000..abf3a0315 --- /dev/null +++ b/test/test-project/hello-async.rhtml @@ -0,0 +1,3 @@ + + Hello $name! + \ No newline at end of file diff --git a/test/test-project/hello-error.rhtml b/test/test-project/hello-error.rhtml new file mode 100644 index 000000000..598094f42 --- /dev/null +++ b/test/test-project/hello-error.rhtml @@ -0,0 +1 @@ +{% throw new Error('Test'); %} \ No newline at end of file