deface

node.js/express port of spree/deface. Allows overriding of HTML without partials.

npm install deface
2 downloads in the last week
4 downloads in the last month

node-deface

node-deface is a port of https://github.com/spree/deface to node.js and express.

It allows developers to customize node.js HTML views without requiring partials or editing the underlying view.

You can use any template library. node-deface works by extending res.render to look up override scripts and apply them after template rendering.

Overrides use css selectors to find what to replace and use path to determine which view to apply to.

The text inside the override can be specified by whichever template library is passed to deface.override(app,template_library,folder) .

The interface for the template_libray object is:

engine.compile(body) which returns a function(options)

TODO

These need to be done before considering node-deface usable:

  • Add benchmarks to show differences in speed.
  • Add documentation of speed differences.
  • Add support for multiline strings (coffeescript can do this. We can make our overrides coffeescript and the rest plain node.js)
  • (COMPLETED) Replacement text in Deface.override isn't processed as template yet, only string (IMPORTANT for making this usable)
  • (COMPLETED) If you don't have an override for a specific view, the processing fails. (IMPORTANT for making this usable)
  • (COMPLETED) Overrides are loaded every request. They should only be loaded at startup. (IMPORTANT for speed)

Usage

overrides/index_override.js:

module.exports = function(Deface) {
    return Deface.override({
        path: "index", //view name without the file extension.
        replace: "[data-hook=...]", // any CSS selector.
        text: "<p>Title: {{title}}</p>" // any HTML text.
    })
}

views/index.html:

<div>
    <p>Welcome to deface. Title: {{title}}</p>
    <div data-hook="...">
        <p>This content will be overriden.</p>
    </div>
</div>

Express

app.js: var template_lib = require('any-template-library') var deface = require('deface')

app.configure(function(){
    ...
//attach deface middleware using folder name 'overrides'
    app.use(deface.override(app,template_lib,'overrides'))         
...
    app.use(app.router);
    ...
});
npm loves you