brunch/handlebars-brunch — pull request #21 by elving (2013-11-23 22:29:57+00:00)
Title: Add overrides option to override handlebars public API functions.

This will let you override handlebars [public API functions](https://github.com/wycats/handlebars.js/blob/7f6ef1dd38794f12aee33c76c04f604a7651810b/lib/handlebars/compiler/javascript-compiler.js#L10) in your brunch-config file.

### Usage
exports.config =
plugins:
handlebars:
overrides: (handlebars) ->
# Override public methods.

### Use Case
I wanted to be able to pass a `Backbone.Model` directly to a `Handlebars` template and use it's attributes and methods inside the template. This would be possible by overriding the `nameLookup` method in `Handlebars`:

handlebars.JavaScriptCompiler::nameLookup = (parent, name, type) ->
if /^[0-9]+$/.test(name)
"#{parent}[#{name}]"
else if handlebars.JavaScriptCompiler.isValidJavaScriptVariableName(name)
"(typeof #{parent}.get == 'function' ? #{parent}.get('#{name}') : #{parent}.#{name})"
else
"#{parent}[#{name}]"

Now I could do this:

class User extends Backbone.Model
fullName: ->
"#{@get 'firstName'} #{@get 'lastName'}"

Me = new User
firstName: 'Elving'
lastName: 'Rodriguez'
occupation: 'Frontend Developer'

# Template
Hi! My name is {{fullName}} and I am a {{occupation}}.

# Rendering the template by passing the model would print.
Hi! My name is Elving Rodriguez and I am a Frontend Developer.

To do this in `Brunch` I would add this to my `brunch-config`:

exports.config =
plugins:
handlebars:
overrides: (handlebars) ->
handlebars.JavaScriptCompiler::nameLookup = (parent, name, type) ->
if /^[0-9]+$/.test(name)
"#{parent}[#{name}]"
else if handlebars.JavaScriptCompiler.isValidJavaScriptVariableName(name)
"(typeof #{parent}.get == 'function' ? #{parent}.get('#{name}') : #{parent}.#{name})"
else
"#{parent}[#{name}]"