ablock

Asynchronous block-based templating

ABlock - Asynchronous Block Templating

Split your templates into asynchronous blocks, then asynchronously render the template. This is a asynchronous, server-side extension of block.

This is similar to hyperstream except:

  • More than just streams are allowed
  • No CSS selector support - ablock is template type agnostic. You can stream JSON if you really want.

Creates a block instance from an input string. Each block can only be a word and must be surrounded by {{}}. Example, {{header}} is okay, but not {{body.header}}.

Permanently replaces a block with a string or an asynchronous function. block.local() and block.locals() are aliases of each other. For example:

var template = new Block('<html>{{head}}{{body}}</html>')
 
// This would permanently replace {{head}} with the following string 
template.local('head', '<title>My Site</title>')
 
// This would permanently replace {{body}} with an asynchronous template 
template.local('body', function (done) {
  res.render('homepage', done)
})
 
// This would set both locals at the same time: 
template.local({
  head: '<title>My Site</title>',
  bodyfunction (done) {
    res.render('homepage', done)
  }
})

This is useful when building templates from pieces.

This returns a readable stream.

locals are optional, temporary locals for the template. Unlike block.local(), these locals are not permanent. locals must be an object.

callback is an optional callback that returns the resulting template as a Buffer instance. For example:

function (reqresnext) {
  block.render(res.locals, function (errbuf) {
    if (err)
      return next(err)
    if (!buf)
      // One of the underlying streams was destroyed. 
      return next(new Error('Something wrong happened.'))
 
    res.setHeader('Content-Type', 'text/html; charset=utf-8')
    res.send(buf)
  })
}

If you want the result in a single string, just call buf.toString('utf8'). However, this is pretty much unnecessary.

If no callback is set, you are expected to block.render().pipe() into a writable stream. For example:

function (reqres) {
  res.setHeader('Content-Type', 'text/html; charset=utf-8')
  block.render(res.locals).pipe(res)
}

If you want to use conditional GETs (ie send 304 status codes when possible), you should use a callback. Otherwise, you should stream it.

Each block can be:

  • A string
  • A buffer (ASCII or UTF-8)
  • A readable stream
  • A thunk (function that only takes a callback)

The MIT License (MIT)

Copyright (c) 2013 Jonathan Ong me@jongleberry.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.