Skip to content

Commit

Permalink
Merge pull request #4 from Faldrian/addedConfigurationSupport
Browse files Browse the repository at this point in the history
Added configuration support
  • Loading branch information
Brendan Loudermilk committed Feb 21, 2015
2 parents fcbcf7d + 8dcadb0 commit 47a212c
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 75 deletions.
32 changes: 32 additions & 0 deletions README.md
Expand Up @@ -57,6 +57,38 @@ exports.config =
outputDirectory: 'app/another_directory'
```

### Additional project-specific configuration

You can set the context and configure Handlebars in your project by using an extra file (default: `./staticHandlebarsExtras.js` in your projects root directory).

```coffee
exports.config =
plugins:
staticHandlebars:
includeFile: 'app/another_directory'
```

The file may look like this:
```coffee
/*
Parameters:
- handlebars -> reference to the handlebars object

Returns:
- context -> object which represents the context in which the templates will be executed
*/

module.exports = function(handlebars) {
var context = {
color: 'blue'
};

// make calls on the 'handlebars'-variable to define additional helpers etc.

return context;
};
```

## TODO

This library has a long way to go in terms of configurability and compatability
Expand Down
151 changes: 77 additions & 74 deletions lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion src/index.coffee
Expand Up @@ -12,6 +12,12 @@ module.exports = class StaticHandlebarsCompiler
constructor: (@config) ->
@outputDirectory = @config?.plugins?.staticHandlebars?.outputDirectory || 'public'

# Load additional configuration / context from a project-specific file
includeFile = @config?.plugins?.staticHandlebars?.includeFile
if includeFile?
absoluteFile = sysPath.join(sysPath.resolve('./'), includeFile)
@extras = require(absoluteFile)

withPartials: (callback) ->
partials = {}
errThrown = false
Expand All @@ -36,12 +42,13 @@ module.exports = class StaticHandlebarsCompiler
try
basename = sysPath.basename(path, ".hbs")
template = handlebars.compile(data)
context = if @extras then @extras(handlebars) else {}

@withPartials (err, partials) =>
if err?
callback(err)
else
html = template({}, partials: partials, helpers: @makeHelpers(partials))
html = template(context, partials: partials, helpers: @makeHelpers(partials))
newPath = @outputDirectory + path.slice(13, -4) + ".html"

mkdirp.sync(sysPath.dirname(newPath))
Expand Down

0 comments on commit 47a212c

Please sign in to comment.