From 90f7dbf8c496d7c65d776042472fdbac646ef0d0 Mon Sep 17 00:00:00 2001 From: icma Date: Wed, 4 Feb 2015 16:45:05 -0800 Subject: [PATCH] Support $global in template.renderSync * https://github.com/raptorjs/marko/issues/28 --- README.md | 31 +++++++++++++++++++++++++++- runtime/marko-runtime.js | 8 +++---- test/api-tests.js | 29 +++++++++++++++++++++++--- test/test-project/hello-global.marko | 1 + 4 files changed, 60 insertions(+), 9 deletions(-) create mode 100644 test/test-project/hello-global.marko diff --git a/README.md b/README.md index a79fa6467..5e02a5f75 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,7 @@ Syntax highlighting available for [Atom](https://atom.io/) by installing the [la - [Comments](#comments) - [Whitespace](#whitespace) - [Helpers](#helpers) + [Global Properties](#global-properties) - [Custom Tags and Attributes](#custom-tags-and-attributes) - [Async Taglib](#async-taglib) - [Layout Taglib](#layout-taglib) @@ -642,7 +643,7 @@ Input data passed to a template is made available using a special `data` variabl ``` -To asign a new value to an existing variable the `` tag can be used as shown in the following sample code: +To assign a new value to an existing variable the `` tag can be used as shown in the following sample code: ```html @@ -1232,6 +1233,34 @@ Usage inside template:
${data.reverse('reverse test')}
``` +## Global Properties + +The `$global` property is used to add data that is available to all templates encountered during rendering by having the data hang off the wrapped writer. + +```javascript +template.render({ + $global: { + name: 'Frank' + } +}, res); +``` + +Given the following template: + +```html +
+ Hello ${out.global.name}! +
+``` + +The output would be the following: + +```html +
+ Hello Frank +
+``` + ## Custom Tags and Attributes Marko supports extending the language with custom tags and attributes. A custom tag or a custom attribute __must have at least one dash__ to indicate that is not part of the standard HTML grammar. diff --git a/runtime/marko-runtime.js b/runtime/marko-runtime.js index 03e6fcbcb..d372c96fb 100644 --- a/runtime/marko-runtime.js +++ b/runtime/marko-runtime.js @@ -66,6 +66,7 @@ Template.prototype = { renderSync: function(data) { var out = new AsyncWriter(); out.sync(); + out.global = extend(out.global, data.$global); this._(data, out); out.end(); return out.getOutput(); @@ -120,10 +121,7 @@ Template.prototype = { shouldEnd = true; } - var $global = data.$global; - if ($global) { - extend(out.global, $global); - } + out.global = extend(out.global, data.$global); renderFunc(data, out); @@ -236,4 +234,4 @@ exports.createWriter = function(writer) { exports.helpers = helpers; -exports.Template = Template; \ No newline at end of file +exports.Template = Template; diff --git a/test/api-tests.js b/test/api-tests.js index cf5b316c9..c88337739 100644 --- a/test/api-tests.js +++ b/test/api-tests.js @@ -81,8 +81,6 @@ describe('marko/api' , function() { }); it('should allow a template to be rendered to a stream', function(done) { - - var output = ''; var outStream = through(function write(data) { output += data; @@ -196,6 +194,32 @@ describe('marko/api' , function() { expect(output).to.equal('Hello John!'); }); + it('should allow a template to be rendered synchronously using global attributes', function() { + var template = marko.load(nodePath.join(__dirname, 'test-project/hello-global.marko')); + var data = { + name: 'John', + $global: { + greeting: 'Greetings' + } + }; + var output = template.renderSync(data) + expect(output).to.equal('Greetings John!'); + }); + + it('should allow a template to be rendered asynchronously using global attributes', function(done) { + var template = marko.load(nodePath.join(__dirname, 'test-project/hello-global.marko')); + var data = { + name: 'John', + $global: { + greeting: 'Greetings' + } + }; + template.render(data, function(error, output) { + expect(output).to.equal('Greetings John!'); + done(); + }); + }); + it('should throw an error if beginAsync is used with renderSync', function() { var template = marko.load(nodePath.join(__dirname, 'test-project/hello-async.marko')); var output; @@ -233,4 +257,3 @@ describe('marko/api' , function() { }); }); - diff --git a/test/test-project/hello-global.marko b/test/test-project/hello-global.marko new file mode 100644 index 000000000..a5d65c4d3 --- /dev/null +++ b/test/test-project/hello-global.marko @@ -0,0 +1 @@ +$out.global.greeting $data.name!