Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
Cleanup and fix tests
Revert options to support current rendr#master
Update bundle dependencies
Switch to lodash's underscore compat version
  • Loading branch information
Steve Mclelland committed Dec 11, 2013
1 parent 0038718 commit 12c8dad
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 300 deletions.
264 changes: 38 additions & 226 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,256 +1,68 @@
# grunt-contrib-handlebars v0.6.0 [![Build Status](https://travis-ci.org/gruntjs/grunt-contrib-handlebars.png?branch=master)](https://travis-ci.org/gruntjs/grunt-contrib-handlebars)

> Precompile Handlebars templates to JST file.
# rendr-emblem v0.0.1

## Getting Started
This plugin requires Grunt `~0.4.0`

If you haven't used [Grunt](http://gruntjs.com/) before, be sure to check out the [Getting Started](http://gruntjs.com/getting-started) guide, as it explains how to create a [Gruntfile](http://gruntjs.com/sample-gruntfile) as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

```shell
npm install grunt-contrib-handlebars --save-dev
```

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

```js
grunt.loadNpmTasks('grunt-contrib-handlebars');
```

*This plugin was designed to work with Grunt 0.4.x. If you're still using grunt v0.3.x it's strongly recommended that [you upgrade](http://gruntjs.com/upgrading-from-0.3-to-0.4), but in case you can't please use [v0.3.3](https://github.com/gruntjs/grunt-contrib-handlebars/tree/grunt-0.3-stable).*



## Handlebars task
_Run this task with the `grunt handlebars` command._

Task targets, files and options may be specified according to the grunt [Configuring tasks](http://gruntjs.com/configuring-tasks) guide.
### Options

#### separator
Type: `String`
Default: `linefeed + linefeed`

Concatenated files will be joined on this string.

#### namespace
Type: `String` `false`
Default: `'JST'`

The namespace in which the precompiled templates will be assigned. *Use dot notation (e.g. App.Templates) for nested namespaces or false for no namespace wrapping.* When false with `amd` option set `true`, templates will be returned directly from the AMD wrapper.

Example:
```js
options: {
namespace: 'MyApp.Templates'
}
```

#### partialsUseNamespace
Type: `Boolean`
Default: `false`

When set to `true`, partials will be registered in the `namespace` in addition to templates.

#### wrapped
Type: `Boolean`
Default: `true`

Determine if preprocessed template functions will be wrapped in Handlebars.template function.

#### node
Type: `Boolean`
Default: `false`

Enable the compiled file to be required on node.js by preppending and appending proper declarations. You can use the file safely on the front-end.

For this option to work you need to define the `namespace` option.

#### amd
Type: `Boolean`
Default: `false`

Wraps the output file with an AMD define function and returns the compiled template namespace unless namespace has been explicitly set to false in which case the template function will be returned directly.
## Getting Started

```js
define(function() {
//...//
return this['[template namespace]'];
});
```
[Handlebars](http://handlebarsjs.com/) and [Emblem](http://emblemjs.com/) template adapter for [Rendr](https://github.com/airbnb/rendr) apps.

#### commonjs
Type: `Boolean`
Default: `false`

Wraps the output file in a CommonJS module function, exporting the compiled templates. It will also add templates to the template namespace, unless `namespace` is explicitly set to `false`.

```js
module.exports = function(Handlebars) {
//...//
Handlebars.template(…);
return this['[template namespace]'];
};
```
## Usage

When requiring the module in a CommonJS environment, pass in your `Handlebars` object.

```js
var Handlebars = require('handlebars');
var templates = require('./templates')(Handlebars);
```

#### processContent
Type: `function`
### 1) Set the default templateAdapter to rendr-emblem

This option accepts a function which takes two arguments (the template file content, and the filepath) and returns a string which will be used as the source for the precompiled template object. The example below removes leading and trailing spaces to shorten templates.
./app/app.js

```js
options: {
processContent: function(content, filepath) {
content = content.replace(/^[\x20\t]+/mg, '').replace(/[\x20\t]+$/mg, '');
content = content.replace(/^[\r\n]+/, '').replace(/[\r\n]*$/, '\n');
return content;
}
}
```

#### processAST
Type: `function`

This option accepts a function which takes one argument (the parsed Abstract Syntax Tree) and returns a modified version which will be used as the source for the precompiled template object. The example below removes any partial and replaces it with the text `foo`.

```js
options: {
processAST: function(ast) {
ast.statements.forEach(function(statement, i) {
if (statement.type === 'partial') {
ast.statements[i] = { type: 'content', string: 'foo' };
}
module.exports = BaseApp.extend({
defaults: {
templateAdapter: 'rendr-emblem'
},
...
});
return ast;
}
}
```

#### processName
Type: `function`

This option accepts a function which takes one argument (the template filepath) and returns a string which will be used as the key for the precompiled template object. The example below stores all templates on the default JST namespace in capital letters.

```js
options: {
processName: function(filePath) {
return filePath.toUpperCase();
}
}
```

#### processPartialName
Type: `function`

This option accepts a function which takes one argument (the partial filepath) and returns a string which will be used as the key for the precompiled partial object when it is registered in Handlebars.partials. The example below stores all partials using only the actual filename instead of the full path.

```js
options: {
processPartialName: function(filePath) { // input: templates/_header.hbs
var pieces = filePath.split("/");
return pieces[pieces.length - 1]; // output: _header.hbs
}
}
````

Note: If processPartialName is not provided as an option the default assumes that partials will be stored by stripping trailing underscore characters and filename extensions. For example, the path *templates/_header.hbs* will become *header* and can be referenced in other templates as *{{> header}}*.

#### partialRegex
Type: `Regexp`
Default: `/^_/`
### 2) Create emblem files

This option accepts a regex that defines the prefix character that is used to identify Handlebars partial files.

``` javascript
// assumes partial files would be prefixed with "par_" ie: "par_header.hbs"
options: {
partialRegex: /^par_/
}
```
Create .emblem files within your ./app/views. You can intermix .hbs and .emblem within the same folder. If the names are the same, the .emblem takes precedence.

#### partialsPathRegex
Type: `Regexp`
Default: `/./`

This option accepts a regex that defines the path to a directory of Handlebars partials files. The example below shows how to mark every file in a specific directory as a partial.

``` javascript
options: {
partialRegex: /.*/,
partialsPathRegex: /\/partials\//
}
```

#### compilerOptions
Type `Object`
Default: `{}`
### 3) Add grunt task

This option allows you to specify a hash of options which will be passed directly to the Handlebars compiler.
- Install the grunt task
[grunt-emblem-handlebars](https://github.com/modalstudios/grunt-emblem-handlebars.git)

``` javascript
options: {
compilerOptions: {
knownHelpers: {
"my-helper": true,
"another-helper": true
},
knownHelpersOnly: true
}
}
```

### Usage Examples
- Switch out your usual Rendr [handlebars] task with a slightly modified version. This task will pre-compile both Handlebars and Emblem down to a single pre-compiled Handlebars.

```js
handlebars: {
compile: {
options: {
namespace: "JST"
},
files: {
"path/to/result.js": "path/to/source.hbs",
"path/to/another.js": ["path/to/sources/*.hbs", "path/to/more/*.hbs"]
- ```
emblem: {
compile: {
options: {
namespace: false,
commonjs: true,
processName: function(filename) {
var r;
r = /(apps\/app\/(templates|views)\/)/;
return filename.replace(r, '').replace(/(.emblem|.hbs)/, '');
}
},
files: {
"tmp/compiledTemplates.js": ["apps/app/views/**/*.emblem", "apps/app/views/**/*.hbs"]
},
filter: function(filepath) {
var filename;
filename = path.basename(filepath);
return filename.slice(0, 2) !== "__";
}
}
}
}
```


## Release History

* 2013-11-11   v0.6.0   Update handlebars dep to ~1.1.2
* 2013-11-07   v0.5.12   Pass file path into `processContent`.
* 2013-09-24   v0.5.11   Fix for broken partial pre-compilation.
* 2013-07-14   v0.5.10   Add `commonjs` option.
* 2013-05-30   v0.5.9   Allow passing `compilerOptions` to Handlebars compiler.
* 2013-03-14   v0.5.8   Update handlebars dep to ~1.0.10
* 2013-03-11   v0.5.7   Fix regression with 'wrapped' option.
* 2013-03-07   v0.5.6   Add new option, processAST
* 2013-02-27   v0.5.5   Add new options partialsUseNamespace, partialRegex, partialsPathRegex
* 2013-02-15   v0.5.4   First official release for Grunt 0.4.0.
* 2013-02-08   v0.5.4rc7   When `namespace` is false and `amd` is true, return handlebars templates directly from AMD wrapper.
* 2013-02-01   v0.5.3rc7   Add `node` option to produce dual node.js / front-end compiled file.
* 2013-01-29   v0.5.2rc7   Define handlebars as a dependency for AMD option.
* 2013-01-28   v0.5.1rc7   Add AMD compilation option. Add processContent option. Do not generate templates into a namespaces when namespace option is false.
* 2013-01-23   v0.5.0rc7   Updating grunt/gruntplugin dependencies to rc7. Changing in-development grunt/gruntplugin dependency versions from tilde version ranges to specific versions. Default wrapped option to true.
* 2013-01-09   v0.4.0rc5   Updating to work with grunt v0.4.0rc5. Switching to this.files api.
* 2012-11-21   v0.3.3   Reset for each target
* 2012-10-12   v0.3.2   Rename grunt-contrib-lib dep to grunt-lib-contrib.
* 2012-10-03   v0.3.1   Bugfix default processPartialName.
* 2012-09-23   v0.3.0   Options no longer accepted from global config key.
* 2012-09-16   v0.2.3   Support for nested namespaces.
* 2012-09-12   v0.2.2   Escape single quotes in filenames.
* 2012-09-10   v0.2.0   Refactored from grunt-contrib into individual repo.

---

Task submitted by [Tim Branyen](http://tbranyen.com)

*This file was generated on Mon Nov 11 2013 12:54:56.*
9 changes: 2 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@ var Handlebars = require('handlebars');

// require('ember/handlebars');
module.exports = function(options){

// Adapter config
this.options = options || {
compiledTemplatesFile: ''
};

var localExports, templateFinder;
this.options = options

localExports = {};
templateFinder = require('./shared/templateFinder')(Handlebars);
Expand All @@ -32,7 +27,7 @@ module.exports = function(options){
* The default pattern `/.+/` is very greedy; it matches anything, including nested paths.
* Expose `templatePatterns` for manipulating how `getTemplate` finds templates.
*/
localExports.templatePatterns.push({pattern: /.+/, src: this.options.compiledTemplatesFile})
localExports.templatePatterns.push({pattern: /.+/, src: options.entryPath + 'app/templates/compiledTemplates'})

/**
* `getLayout` should only be used on the server.
Expand Down
19 changes: 9 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
{
"name": "rendr-handlebars",
"version": "0.1.0",
"description": "Glue handlebars templates into a Rendr app.",
"name": "rendr-emblem",
"version": "0.1.1",
"description": "Glue emblem/handlebars templates into a Rendr app.",
"main": "index.js",
"scripts": {
"test": "mocha test/*.test.js test/**/*.test.js"
},
"repository": {
"type": "git",
"url": "http://github.com/airbnb/rendr-handlebars.git"
"url": "http://github.com/modalstudios/rendr-emblem.git"
},
"dependencies": {
"underscore": "~1.4.4",
"emblem": "~0.3.4",
"handlebars":"~1.0.12"
},
"peerDependencies": {
"rendr": ">=0.5.0-alpha09"
"lodash": "~2.4",
"emblem": "*",
"handlebars": "1.0.12"
},
"bundledDependencies": [
],
"keywords": [
"rendr",
"handlebars",
Expand Down
16 changes: 10 additions & 6 deletions server/layoutFinder.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@ var Emblem = require('emblem');
module.exports = function(Handlebars) {

return {
getLayout: function(name, options, callback) {
getLayout: function(name, entryPath, callback) {

// Check if an emblem version exists, if so use it, otherwise fallback to .hbs
var layoutExt = 'emblem';
var layoutFile = path.join(options.templatePath, name + '.' + layoutExt);

var getLayoutFile = function(ext) {
return path.join(entryPath, 'app/templates/', name + '.' + ext);
}

var layoutFile = getLayoutFile('emblem');
if (!fs.existsSync(layoutFile)) {
layoutExt = 'hbs';
layoutFile = path.join(options.templatePath, name + '.' + layoutExt);
if (!fs.existsSync(layoutFile)) {
throw new Error("Couldn't find template: [" + layoutFile + " or .emblem]");

layoutFile = getLayoutFile('hbs');
if (!fs.existsSync(layoutFile('hbs'))) {
throw new Error("rendr-emblem - Couldn't find template: [" + layoutFile + " or .emblem]");
}
}

Expand Down
11 changes: 10 additions & 1 deletion shared/helpers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
var _ = require('underscore');
var _ = null;

// a bit of a hack because rendr relies on underscore and we want to migrate to lodash's drop in replacement
// in order to make the name's compatible, we need to use 'underscore' when included in the client
//
if (typeof window !== "undefined" && window !== null) {
_ = require('underscore');
} else {
_ = require('lodash/dist/lodash.underscore')
}

// Lazy-required.
var BaseView = null;
Expand Down
Loading

0 comments on commit 12c8dad

Please sign in to comment.