` 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!