Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

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

branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

README.md

node-deface

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

You can now find us in npm!

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)
  • Add gruntfile build script.
    • Add mocha tests.
    • Add to travis-ci.
  • Add LICENSE
  • (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)

These are some other TODOs which are pluses but not required:

  • Port to coffeescript?
  • Add JSON or yaml support for deface override files.
  • Add precompilation of templates + overrides where supported.
  • Create gh-pages.

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);
    ...
});
Something went wrong with that request. Please try again.