Skip to content

Commit

Permalink
Support $global in template.renderSync
Browse files Browse the repository at this point in the history
* #28
  • Loading branch information
icma committed Feb 5, 2015
1 parent 872c5c4 commit 90f7dbf
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 9 deletions.
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -642,7 +643,7 @@ Input data passed to a template is made available using a special `data` variabl
<var name="name" value="data.name.toUpperCase()" />
```

To asign a new value to an existing variable the `<assign>` tag can be used as shown in the following sample code:
To assign a new value to an existing variable the `<assign>` tag can be used as shown in the following sample code:

```html
<assign var="name" value="data.name.toLowerCase()" />
Expand Down Expand Up @@ -1232,6 +1233,34 @@ Usage inside template:
<div>${data.reverse('reverse test')}</div>
```

## 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
<div>
Hello ${out.global.name}!
</div>
```

The output would be the following:

```html
<div>
Hello Frank
</div>
```

## 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.
Expand Down
8 changes: 3 additions & 5 deletions runtime/marko-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -236,4 +234,4 @@ exports.createWriter = function(writer) {

exports.helpers = helpers;

exports.Template = Template;
exports.Template = Template;
29 changes: 26 additions & 3 deletions test/api-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -233,4 +257,3 @@ describe('marko/api' , function() {
});

});

1 change: 1 addition & 0 deletions test/test-project/hello-global.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$out.global.greeting $data.name!

0 comments on commit 90f7dbf

Please sign in to comment.