How to pass a readable stream of objects to the Handlebars template

For example, I use the npm account module , which has a method list()for displaying all accounts. list()returns a readable stream of objects, so my current solution is to create a list from this stream and pass it to my template.

Is there a way for Handlebars to directly read a stream and not read this stream in memory as a list, so it can be read from my Handlebars template?

Here is an example of my situation:

Here I am creating a list resultsfrom the stream of my accounts stream, so I can pass it to my template account-list-template:

var results = [];
var stream = server.accounts.list();

stream
  .on('data', function (data) {
    results.push(data);
  })
  .on('error', function (err) {
    return console.log(err);
  })
  .on('end', function () {
    var ctx = {accounts: results};
    return response().html(server.render('account-list-template', ctx)).pipe(res);
  });

handlebars-stream, . , :

return server.accounts.list().pipe(handlebars(server.render('account-list-template'))).pipe(res); // produces x times the template

, , x x .

:

{{#extend "layout"}}

{{#replace "body"}}
<div class="container">
    <hr>
    <div class="sheet-list">
        <h1>Master account list:</h1>
        {{#each accounts}}
        <div class="list-item clearfix">
            <a href="/account/edit/{{ key }}">
                <div class="list-item-info">
                    {{ key }}
                    {{ value.email }}
                    <h3 class="list-item-name">{{ key }}</h3>
                    <div class="list-item-description">{{ value.email }}</div>

                </div>

                <div class="list-item-actions">
                    <form action="/account/delete/{{ key }}" method="post">
                        <button type="submit" class="destroy-sheet">
                            <i class="fa fa-trash-o"></i> destroy
                        </button>
                    </form>
                </div>
            </a>
        </div>
        {{/each }}
    </div>

</div>

{{/replace}}

{{/extend}}
+4

All Articles