Merge pull request #30 from ianma/fix-render-sync-global

Support $global in template.renderSync
This commit is contained in:
Patrick Steele-Idem 2015-02-04 17:58:28 -07:00
commit 323641dc82
4 changed files with 60 additions and 9 deletions

View File

@ -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
<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()" />
@ -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.

View File

@ -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);

View File

@ -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() {
});
});

View File

@ -0,0 +1 @@
$out.global.greeting $data.name!