mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
Support $global in template.renderSync
* https://github.com/raptorjs/marko/issues/28
This commit is contained in:
parent
872c5c4c96
commit
90f7dbf8c4
31
README.md
31
README.md
@ -60,6 +60,7 @@ Syntax highlighting available for [Atom](https://atom.io/) by installing the [la
|
|||||||
- [Comments](#comments)
|
- [Comments](#comments)
|
||||||
- [Whitespace](#whitespace)
|
- [Whitespace](#whitespace)
|
||||||
- [Helpers](#helpers)
|
- [Helpers](#helpers)
|
||||||
|
[Global Properties](#global-properties)
|
||||||
- [Custom Tags and Attributes](#custom-tags-and-attributes)
|
- [Custom Tags and Attributes](#custom-tags-and-attributes)
|
||||||
- [Async Taglib](#async-taglib)
|
- [Async Taglib](#async-taglib)
|
||||||
- [Layout Taglib](#layout-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()" />
|
<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
|
```html
|
||||||
<assign var="name" value="data.name.toLowerCase()" />
|
<assign var="name" value="data.name.toLowerCase()" />
|
||||||
@ -1232,6 +1233,34 @@ Usage inside template:
|
|||||||
<div>${data.reverse('reverse test')}</div>
|
<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
|
## 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.
|
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.
|
||||||
|
|||||||
@ -66,6 +66,7 @@ Template.prototype = {
|
|||||||
renderSync: function(data) {
|
renderSync: function(data) {
|
||||||
var out = new AsyncWriter();
|
var out = new AsyncWriter();
|
||||||
out.sync();
|
out.sync();
|
||||||
|
out.global = extend(out.global, data.$global);
|
||||||
this._(data, out);
|
this._(data, out);
|
||||||
out.end();
|
out.end();
|
||||||
return out.getOutput();
|
return out.getOutput();
|
||||||
@ -120,10 +121,7 @@ Template.prototype = {
|
|||||||
shouldEnd = true;
|
shouldEnd = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
var $global = data.$global;
|
out.global = extend(out.global, data.$global);
|
||||||
if ($global) {
|
|
||||||
extend(out.global, $global);
|
|
||||||
}
|
|
||||||
|
|
||||||
renderFunc(data, out);
|
renderFunc(data, out);
|
||||||
|
|
||||||
@ -236,4 +234,4 @@ exports.createWriter = function(writer) {
|
|||||||
|
|
||||||
exports.helpers = helpers;
|
exports.helpers = helpers;
|
||||||
|
|
||||||
exports.Template = Template;
|
exports.Template = Template;
|
||||||
|
|||||||
@ -81,8 +81,6 @@ describe('marko/api' , function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should allow a template to be rendered to a stream', function(done) {
|
it('should allow a template to be rendered to a stream', function(done) {
|
||||||
|
|
||||||
|
|
||||||
var output = '';
|
var output = '';
|
||||||
var outStream = through(function write(data) {
|
var outStream = through(function write(data) {
|
||||||
output += data;
|
output += data;
|
||||||
@ -196,6 +194,32 @@ describe('marko/api' , function() {
|
|||||||
expect(output).to.equal('Hello John!');
|
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() {
|
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 template = marko.load(nodePath.join(__dirname, 'test-project/hello-async.marko'));
|
||||||
var output;
|
var output;
|
||||||
@ -233,4 +257,3 @@ describe('marko/api' , function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
1
test/test-project/hello-global.marko
Normal file
1
test/test-project/hello-global.marko
Normal file
@ -0,0 +1 @@
|
|||||||
|
$out.global.greeting $data.name!
|
||||||
Loading…
x
Reference in New Issue
Block a user