Socket
Socket
Sign inDemoInstall

ablock

Package Overview
Dependencies
1
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

ablock

Asynchronous block-based templating


Version published
Maintainers
1
Weekly downloads
1

Weekly downloads

Readme

Source

ABlock - Asynchronous Block Templating Build Status

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.

API

new Block(string)

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}}.

block.local({} || [key'', value]), block.locals()

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>',
  body: function (done) {
    res.render('homepage', done)
  }
})

This is useful when building templates from pieces.

block.render([locals], [callback])

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 (req, res, next) {
  block.render(res.locals, function (err, buf) {
    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 (req, res) {
  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.

Valid block types

Each block can be:

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

License

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.

FAQs

Last updated on 02 Oct 2013

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc