mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
Added support for rendering to an existing stream
This commit is contained in:
parent
76eeca307f
commit
52d7457f1c
23
README.md
23
README.md
@ -237,6 +237,7 @@ template.render({
|
|||||||
```
|
```
|
||||||
|
|
||||||
### Streaming API
|
### Streaming API
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var template = require('raptor-templates').load('template.rhtml');
|
var template = require('raptor-templates').load('template.rhtml');
|
||||||
var out = require('fs').createWriteStream('index.html', 'utf8');
|
var out = require('fs').createWriteStream('index.html', 'utf8');
|
||||||
@ -249,6 +250,19 @@ template.stream({
|
|||||||
.pipe(out);
|
.pipe(out);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Alternatively, you can render directly to an existing stream to avoid creating an intermediate stream:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
var template = require('raptor-templates').load('template.rhtml');
|
||||||
|
var out = require('fs').createWriteStream('index.html', 'utf8');
|
||||||
|
|
||||||
|
// Render the template to 'index.html'
|
||||||
|
template.render({
|
||||||
|
name: 'Frank',
|
||||||
|
count: 30
|
||||||
|
}, out);
|
||||||
|
```
|
||||||
|
_NOTE:_ This will end the target output stream.
|
||||||
|
|
||||||
### Asynchronous Render Context API
|
### Asynchronous Render Context API
|
||||||
|
|
||||||
@ -1271,23 +1285,22 @@ __Answer__: The runtime for template rendering is supported in all web browsers.
|
|||||||
|
|
||||||
__Question:__ _How can Raptor Templates be used with Express?_
|
__Question:__ _How can Raptor Templates be used with Express?_
|
||||||
|
|
||||||
__Answer__: The recommended way to use Raptor Templates with Express is to bypass the Express view engine and instead directly pipe the rendering output stream to the response stream as shown in the following code:
|
__Answer__: The recommended way to use Raptor Templates with Express is to bypass the Express view engine and instead have Raptor Templates render directly to the response stream as shown in the following code:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var template = require('raptor-templates').load(require.resolve('./template.rhtml'));
|
var template = require('raptor-templates').load(require.resolve('./template.rhtml'));
|
||||||
|
|
||||||
app.get('/profile', function(req, res) {
|
app.get('/profile', function(req, res) {
|
||||||
template
|
template
|
||||||
.stream({
|
.render({
|
||||||
name: 'Frank'
|
name: 'Frank'
|
||||||
})
|
}, res);
|
||||||
.pipe(res);
|
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
With this approach, you can benefit from streaming and there is no middleman (less complexity).
|
With this approach, you can benefit from streaming and there is no middleman (less complexity).
|
||||||
|
|
||||||
Alternatively, you can use the independent [view-engine](https://github.com/patrick-steele-idem/view-engine) module to provide an abstraction that allows pluggable view engines and provides full support for streaming. This is shown in the following sample code:
|
Alternatively, you can use the streaming API to produce an intermediate stream that can then be piped to the response stream as shown below:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var template = require('view-engine').load(require.resolve('./template.rhtml'));
|
var template = require('view-engine').load(require.resolve('./template.rhtml'));
|
||||||
|
|||||||
@ -74,7 +74,7 @@ Template.prototype = {
|
|||||||
}
|
}
|
||||||
renderWithCallback(this, data, context, callback);
|
renderWithCallback(this, data, context, callback);
|
||||||
} else {
|
} else {
|
||||||
if (context.attributes) {
|
if (context.isRenderContext) {
|
||||||
this._(data, context);
|
this._(data, context);
|
||||||
} else {
|
} else {
|
||||||
// Assume the "context" is really a stream
|
// Assume the "context" is really a stream
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user