asyncbars

Asynchronous templating

Usage no npm install needed!

<script type="module">
  import asyncbars from 'https://cdn.skypack.dev/asyncbars';
</script>

README

Asyncbars

Build Status

Asynchronous templating engine using Handlebars syntax.

Example

Query your database right from the template!

index.coffee:


Asyncbars = require 'asyncbars'

renderer = Asyncbars.createRenderer
  eachPost: ->
    Posts.find {}, (err, posts) =>
      if err
        Promise.reject(err)
      else
        Promise.all posts.map (post) =>
          # render body of the helper for each post
          # set context variable `title`
          @cb yes, title: post.title
        .then (strs) -> strs.join('')
  title: -> @ctx.title

nodes = Asyncbars.parseFile 'view.hbs'

renderer.render(nodes).then(console.log)

view.hbs:

<ul>
  {{#eachPost}}
    <li>{{title}}</li>
  {{/eachPost}}
</ul>

Example output:

<ul>
  <li>Foo</li>
  <li>Bar</li>
</ul>

See test/ for more examples.

Todos:

  • Allow quoting helper arguments: {{helper "arg" opt="value"}}
  • Allow to {{include file.hbs}}
  • Allow other helpers as helper arguments: {{#if {{isEnabled}} }} class="enabled" {{/if}}